2013-11-22 18 views
21

VS2010(MVC 4)のRazorを使用してC#プロジェクトを行っています。 コントローラからビューにエラーメッセージを返し、それをユーザーに表示する必要があります。私が試みた何 は次のとおりです。コントローラからのMVC 4リターンエラーメッセージ - 表示で表示

CONTROLLER:

[HttpPost] 
public ActionResult form_edit(FormModels model) 
{   
    model.error_msg = model.update_content(model);   
    ModelState.AddModelError("error", "adfdghdghgdhgdhdgda"); 
    ViewBag.error = TempData["error"]; 
    return RedirectToAction("Form_edit", "Form"); 

} 

VIEW:

@model mvc_cs.Models.FormModels 
@using ctrlr = mvc_cs.Controllers.FormController 


@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 

     <table> 
     <tr> 
     <td>  
     @Html.ValidationSummary("error")  
     @Html.ValidationMessage("error")  
     </td> 
     </tr> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.content_name) 
      @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 

     </th> 
    </tr> 
</table> 

<table> 
    <tr> 
     <td> 
      <input type="submit" value="Submit" /> 
     </td> 
    </tr> 
</table> 
} 

これを達成するために私を助けてください。

+0

はあなたが確信しているリダイレクトを返したいと表示されませんか? – Grundy

+0

'RedirectToAction(" Form_edit "、" Form ");' – Grundy

+0

の代わりに 'return View(model)'を試してください。 – Ramesh

答えて

27

リターンビュー(モデル)は、ポストメソッドの値をモデルに入力せず、ドロップダウンのモデルデータが空であるため、エラーが返されます。エラーの表示を管理する方法を詳しく説明するGetメソッドを提供してください。

[HttpPost] 
public ActionResult form_edit(FormModels model) 
{   
    if(ModelState.IsValid()) 
    { 
     --- operations 
     return Redirect("OtherAction", "SomeController"); 
    } 

    // here you can use a little trick 
    //fill the model property that holds the information for the dropdown with the data 

    // you haven't provided the get method but it should look something like this 
    model.Countries = ... some data goes here; 
    model.dd_value = ... some other data; 
    model.dd_text = ... other data; 

    ModelState.AddModelError("", "adfdghdghgdhgdhdgda"); 
    return View(model); 
} 

をして、ビューにだけ使用します:エラーへの順序が示されなければ、あなたはこれを使用する必要があり

@model mvc_cs.Models.FormModels 
@using ctrlr = mvc_cs.Controllers.FormController 


@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 

    <table> 
    <tr> 
    <td>  
    @Html.ValidationSummary(true)    
    </td> 
    </tr> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.content_name) 
     @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 

    </th> 
</tr> 
</table> 

<table> 
<tr> 
    <td> 
     <input type="submit" value="Submit" /> 
    </td> 
</tr> 
</table> 
} 

これは大丈夫動作するはずです。

RedirectToActionを使用すると、getメソッドにリダイレクトされますが、エラーは発生しませんが、ビューは再ロードされ、エラーは表示されません。周り

他の方法は、あなたがModelState.AddErrorでないエラーを渡すことができるということですが、このようなViewDataを[「エラー」]と:

[HttpPost] 
public ActionResult form_edit(FormModels model) 
{   
    TempData["error"] = "someErrorMessage"; 
    return RedirectToAction("form_Post", "Form"); 
} 

[HttpGet] 
public ActionResult form_edit() 
{ 
    do stuff here ---- 
    ViewData["error"] = TempData["error"]; 
    return View(); 
} 

@model mvc_cs.Models.FormModels 
@using ctrlr = mvc_cs.Controllers.FormController 


@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 

    <table> 
    <tr> 
    <td>  
    <div>@ViewData["error"]</div>    
    </td> 
    </tr> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.content_name) 
     @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 

    </th> 
</tr> 
</table> 

<table> 
<tr> 
    <td> 
     <input type="submit" value="Submit" /> 
    </td> 
</tr> 
</table> 
} 
+0

ModelState.AddModelErrorは上司のように機能します! tnx – Reza

3

あなたは、あなたができるリダイレクトを行いたい場合は、次のいずれか

ViewBag.Error = "error message"; 

または

TempData["Error"] = "error message"; 
6

すべての返信をありがとう。

は、私は以下のようにして、この問題を解決することができた:

CONTROLLER:

[HttpPost]  
public ActionResult form_edit(FormModels model) 
{ 
    model.error_msg = model.update_content(model); 
    return RedirectToAction("Form_edit", "Form", model); 
} 

public ActionResult form_edit(FormModels model, string searchString,string id) 
{ 
    string test = model.selectedvalue; 
    var bal = new FormModels(); 
    bal.Countries = bal.get_contentdetails(searchString); 
    bal.selectedvalue = id; 
    bal.dd_text = "content_name"; 
    bal.dd_value = "content_id"; 

    test = model.error_msg; 
    ViewBag.head = "Heading"; 

    if (model.error_msg != null) 
    { 
    ModelState.AddModelError("error_msg", test); 
    } 

    model.error_msg = ""; 
    return View(bal); 
} 

VIEW:

@using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) 
{ 
    <table> 
    <tr> 
     <td> 
     @ViewBag.error 
     @Html.ValidationMessage("error_msg") 
     </td> 
    </tr> 
    <tr> 
     <th> 
     @Html.DisplayNameFor(model => model.content_name) 
     @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") 
     </th> 
    </tr> 
    </table> 
} 
関連する問題