2013-11-22 45 views
21

我在VS2010中使用Razor進行C#項目(MVC 4)。 我需要從控制器返回錯誤消息以查看並顯示給用戶。 什麼我試過是:MVC 4 - 從控制器返回錯誤消息 - 在視圖中顯示

控制器:

[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

Ya,我需要返回RedirectToAction(「Form_edit」,「Form」); – Ramesh

回答

27

返回視圖(模型)返回您的錯誤,因爲您沒有使用post方法中的值填充模型,並且下拉列表的模型數據爲空。請提供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不能通過錯誤,但計算機[「錯誤」]是這樣的:

[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

感謝所有的答覆。

我可以做來解決這個如下:

控制器:

[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> 
} 
相關問題