validation - How to Validate with ASP.NET MVC - Model State is Invalid When Form Submitted -
i trying validate form in mvc.
i add custom errors model state , invalid when form submitted. when view displayed doesn’t show validation messages nor validation summary. can please let me know doing wrong or point me in right direction if there other way of validating?
edit asp.net mvc 1. here's code:
following entity
namespace dcs.dal.entities { public class group : idataerrorinfo { public int groupid { get; set; } public string groupname { ; set; } public string abouttext { get; set; } public string logourl { get; set; } public string friendlyurl { get; set; } public bool excludefromft { get; set; } public contactinfo contactinfo { get; set; } public string error { { return string.empty; } } public string this[string propname] { { if ((propname == "groupname") && string.isnullorempty(groupname)) return "please enter group name"; return null; } } } }
following view
<%= html.validationsummary("please correct following details") %> <% using (html.beginform()) {%> <div id="diverror" style="display:none;"> errors <% foreach (keyvaluepair<string, modelstate> keyvaluepair in viewdata.modelstate) { foreach (modelerror modelerror in keyvaluepair.value.errors) { %> <% response.write(modelerror.errormessage); %> <% } } %> </div> <fieldset> <table> <tr> <td> <label for="groupname">group name:</label> </td> <td> <%= html.textbox("groupname", model.groupname) %> <%= html.validationmessage("groupname","group") %> </td>
foreach loop testing, gets loop doesn’t response.write error message nor validation summary nor validation message.
following controller
[acceptverbs(httpverbs.post)] public actionresult editgroup(group group, formcollection collection) { //group group = new group(); bool success = false; try { var contactinfo = new contactinfo { contactname = collection["contactname"], email = collection["email"], fax = collection["fax"], headofficeaddress = collection["headofficeaddress"], freephone = collection["freephone"], telephone = collection["telephone"], website = collection["website"] }; group.contactinfo = contactinfo; group.groupname = collection["groupname"]; if(string.isnullorempty(group.groupname)) { modelstate.addmodelerror("groupname", "please enter group name"); } if (!modelstate.isvalid) { success = grouprepository.insertupdategroup(group); return view(group); } } catch { } //return json(success); return view(group); }
it go if(!modelstate.isvalid)
loop doesn’t display error.
edit 2 can see in text visualiser validation summary have error message, wont display on screen though.
thanks
you decorate model properties data annotation attributes allowing perform validation logic. here's simplified example:
model:
public class group { [required] public string groupname { get; set; } }
controller:
public class homecontroller : controller { public actionresult index() { return view(new group()); } [acceptverbs(httpverbs.post)] public actionresult index(group group) { // remark: don't need formcollection argument action, // leave default model binder job - work // contactinfo property long name text fields // appropriately. example html.textbox("contactinfo.email", model.contactinfo.email) return view(group); } }
view:
<% using (html.beginform()) { %> <label for="groupname">group name:</label> <%= html.textbox("groupname", model.groupname) %> <%= html.validationmessage("groupname", "group") %> <input type="submit" value="post" /> <% } %>
it's decide whether data annotations sufficient case, bear in mind if need perform more advanced validation scenarios might take @ third party frameworks fluentvalidation , xval.
Comments
Post a Comment