How to Access ModelState Property within the View of ASP.NET MVC ?

Most of the times , the ModelState property is used within the controller to verify if the model has valid data or not. But there are sometimes where you want to access the ModelState Property within the View in ASP.NET MVC Application.

How to Access ModelState Property within the View of ASP.NET MVC ?

If you need to access the ModelState property in the View , you can use the property ViewContext.ViewData.ModelState in your Razor View.

@model MvcApplication1.Models.EmployeeVM

@{

if (@ViewContext.ViewData.ModelState.IsValid)

{

ViewBag.Title = "Index";

}

}

<h2>@ViewBag.ApplicationTitle</h2>

@Html.Label("AbundantCode-a Programming Directory")

@Html.RadioButtonFor(Model => Model.IsActive, true)

@Html.RadioButtonFor(Model => Model.IsActive, false)

@*@Html.RadioButton("radio1", !Model.IsActive)