我在VS2010中使用Razor进行C#项目。(MVC 4)。 我需要将错误消息从Controller返回到View并显示给User。 我的尝试是:
控制器:
[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");
}
查看:
@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 :(得分:28)
返回视图(模型)返回错误,因为您没有使用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方法 - &gt;您将没有错误,但视图将被重新加载,并且不会显示任何错误。
另一种方法是你不能通过ModelState.AddError传递错误,但是像这样使用ViewData [“error”]:
[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>
}
答案 1 :(得分:7)
感谢所有回复。
我能够通过以下方式解决这个问题:
控制器:
[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);
}
查看:
@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>
}
答案 2 :(得分:3)
如果您想进行重定向,可以:
ViewBag.Error = "error message";
或
TempData["Error"] = "error message";
答案 3 :(得分:0)
您可以将其添加到_Layout.cshtml:
@using MyProj.ViewModels;
...
@if (TempData["UserMessage"] != null)
{
var message = (MessageViewModel)TempData["UserMessage"];
<div class="alert @message.CssClassName" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>@message.Title</strong>
@message.Message
</div>
}
然后,如果您想在控制器中抛出错误消息:
TempData["UserMessage"] = new MessageViewModel() { CssClassName = "alert-danger alert-dismissible", Title = "Error", Message = "This is an error message" };
MessageViewModel.cs:
public class MessageViewModel
{
public string CssClassName { get; set; }
public string Title { get; set; }
public string Message { get; set; }
}
注意:使用Bootstrap 4类。