How to determine validity of form field from view file?
I am using the MVC client side validation. I am generating the form fields within the view like this:
<% using (Html.BeginForm(new { useCIM = false }))
{%>
<%: Html.ValidationMessage("auth")%>
<%: Html.TextBoxFor(model => model.Country)%>
<%: Html.TextBoxFor(model => model.Zip)%>
I can access the specific Validation Messages within the view like this:
<%: Html.ValidationMessageFor(model => model.Country)%>
<%: Html.ValidationMessageFor(model => model.Zip)%>
How can I simply determine the validity of a specific f开发者_开发知识库ield?
UPDATE***
When I try to...
<% if (ModelState["Country"].Errors > 0)
I get an error that "System.Web.Mvc.ModelState' is a 'type' but is used like a 'variable'"
Use
ModelState["Fieldname"].Errors.Count > 0;
In .aspx page use
<% if(ViewData.ModelState["Fieldname"].Errors.Count > 0)%>
On client side check for field with CSS class
"input-validation-error"
In addition to what Emmanuel N said, if you simply want to know in the view if the field is valid, you can use this:
ViewData.ModelState.IsValidField("Fieldname")
精彩评论