Please help me to show Error message in the view. My code is given below -
请帮我在视图中显示错误消息。我的代码如下 -
This is my View Model:
这是我的视图模型:
public class DepartmentViewModel
{
//public Department Department { get; set; }
public string DepartmentId { get; set; }
[Required]
public string DepartmentName { get; set; }
public string ManagerMemberId { get; set; }
public bool IsActive { get; set; }
public List<Member> Managers { get; set; }
}
This is Controller:
这是控制器:
public class DepartmentController : Controller
{
//
// GET: /Department/
public ActionResult Index()
{
var adminUserId = (string)Session["UserId"];
var memberId = (string)Session["MemberId"];
List<Department> departments = null;
if (!string.IsNullOrEmpty(adminUserId))
{
departments = new DepartmentManager().GetDepartments(false);
}
else if (!string.IsNullOrEmpty(memberId))
{
var companyId = (int)Session["CompanyId"];
departments = new DepartmentManager().GetDepartments(false, companyId);
}
//var managers = new MemberManager().GetMembersOfManagerCategory(true, );
return View("DepartmentList", departments);
}
public ActionResult Delete(string departmentId)
{
try
{
new DepartmentManager().DeleteDepartment(departmentId);
}
catch (System.Exception exception)
{
ModelState.AddModelError("Error", "Unable to delete department. Try again, and if the problem persists see your system administrator.");
//return RedirectToAction("Index", "Department");
}
return RedirectToAction("Index", "Department");
}
}
This is View:
这是视图:
@model IEnumerable<PDCA.Core.EntityClasses.Department>
@{
ViewBag.Title = "Department List";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container">
@using (Html.BeginForm())
{
<form class="form-horizontal">
<fieldset>
<legend>List of Departments:</legend>
<table border="1" cellspacing="1">
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Company
</th>
<th>
Manager
</th>
<th>
IsActive
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DepartmentId)
</td>
<td>
@Html.DisplayFor(modelItem => item.DepartmentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Company.CompanyName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Manager.MemberName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsActive)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { item.DepartmentId }) |
@Html.ActionLink("Delete", "Delete", new { item.DepartmentId })
</td>
</tr>
}
</table>
<p></p>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<p></p>
<p>
@Html.ValidationMessage("Error");
</p>
</fieldset>
</form>
}
In the view I put @Html.ValidationMessage("Error") but the error message is not shown there.
在视图中我放了@ Html.ValidationMessage(“错误”),但错误消息没有显示在那里。
3 个解决方案
#1
1
You are redirecting, so modelstate is gone. You can save it through tempdata, but I wouldn't go that way (How do I maintain ModelState errors when using RedirectToAction?). You can show error with special field and some notification message (eg. toastr), or simply stay on the same view and then modelstate errors will show.
你正在重定向,所以模型状态已经消失。你可以通过tempdata保存它,但我不会这样(使用RedirectToAction时如何维护ModelState错误?)。您可以使用特殊字段和一些通知消息(例如toastr)显示错误,或者只是停留在同一视图上,然后将显示模型状态错误。
public ActionResult Delete(string departmentId)
{
try
{
new DepartmentManager().DeleteDepartment(departmentId);
}
catch (System.Exception exception)
{
ModelState.AddModelError("Error", "Unable to delete department. Try again, and if the problem persists see your system administrator.");
//return RedirectToAction("Index", "Department");
YOU SHOULD ADD MESSAGE TO VIEWDATA HERE, OR RETURN SAME VIEW(make delete POST action also)
}
return RedirectToAction("Index", "Department");
}
#2
1
Firstly you have a duplicate form
首先,您有一个重复的表格
@using (Html.BeginForm())
{
<form class="form-horizontal">
@Html.BeginForm
takes care of the form
tags for you.
@ Html.BeginForm为您处理表单标记。
Secondly, try adding @Html.ValidationSummary
like so
其次,尝试添加@ Html.ValidationSummary
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<fieldset>
Thirdly, you need to reference the correct script files. Typically this would be done in a layout page but that is up to you.
第三,您需要引用正确的脚本文件。通常,这将在布局页面中完成,但这取决于您。
Add references to
添加引用
jquery
jquery.validate.js
jquery.validate.unobtrusive.j
CDNs can be found here
CDN可以在这里找到
Assuming you don't enter a DepartmentName
, press submit and you should see the error message.
假设您没有输入DepartmentName,请按提交,您应该会看到错误消息。
Bear in mind that the model
for your page has to be DepartmentViewModel
. In your example above it isn't.
请记住,页面的模型必须是DepartmentViewModel。在上面的例子中,它不是。
#3
0
try adding the validation summary as also suggested by @glosrob like
尝试添加@glosrob建议的验证摘要
@using (Html.BeginForm())
{
@Html.ValidationSummary()
...
also use ValidationMessageFor
like
也使用ValidationMessageFor之类的
<td>
@Html.DisplayFor(modelItem => item.DepartmentName)
@Html.ValidationMessageFor(v=>v.DepartmentName)
</td>
#1
1
You are redirecting, so modelstate is gone. You can save it through tempdata, but I wouldn't go that way (How do I maintain ModelState errors when using RedirectToAction?). You can show error with special field and some notification message (eg. toastr), or simply stay on the same view and then modelstate errors will show.
你正在重定向,所以模型状态已经消失。你可以通过tempdata保存它,但我不会这样(使用RedirectToAction时如何维护ModelState错误?)。您可以使用特殊字段和一些通知消息(例如toastr)显示错误,或者只是停留在同一视图上,然后将显示模型状态错误。
public ActionResult Delete(string departmentId)
{
try
{
new DepartmentManager().DeleteDepartment(departmentId);
}
catch (System.Exception exception)
{
ModelState.AddModelError("Error", "Unable to delete department. Try again, and if the problem persists see your system administrator.");
//return RedirectToAction("Index", "Department");
YOU SHOULD ADD MESSAGE TO VIEWDATA HERE, OR RETURN SAME VIEW(make delete POST action also)
}
return RedirectToAction("Index", "Department");
}
#2
1
Firstly you have a duplicate form
首先,您有一个重复的表格
@using (Html.BeginForm())
{
<form class="form-horizontal">
@Html.BeginForm
takes care of the form
tags for you.
@ Html.BeginForm为您处理表单标记。
Secondly, try adding @Html.ValidationSummary
like so
其次,尝试添加@ Html.ValidationSummary
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<fieldset>
Thirdly, you need to reference the correct script files. Typically this would be done in a layout page but that is up to you.
第三,您需要引用正确的脚本文件。通常,这将在布局页面中完成,但这取决于您。
Add references to
添加引用
jquery
jquery.validate.js
jquery.validate.unobtrusive.j
CDNs can be found here
CDN可以在这里找到
Assuming you don't enter a DepartmentName
, press submit and you should see the error message.
假设您没有输入DepartmentName,请按提交,您应该会看到错误消息。
Bear in mind that the model
for your page has to be DepartmentViewModel
. In your example above it isn't.
请记住,页面的模型必须是DepartmentViewModel。在上面的例子中,它不是。
#3
0
try adding the validation summary as also suggested by @glosrob like
尝试添加@glosrob建议的验证摘要
@using (Html.BeginForm())
{
@Html.ValidationSummary()
...
also use ValidationMessageFor
like
也使用ValidationMessageFor之类的
<td>
@Html.DisplayFor(modelItem => item.DepartmentName)
@Html.ValidationMessageFor(v=>v.DepartmentName)
</td>