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.