asp.net mvc - How to create a custom Html.ValidationMessage? -


this controller:

/// <summary> /// activity /// </summary> /// <returns></returns> public actionresult createactivity() {     ariacrmentities aria = new ariacrmentities();     var unit = u in aria.activitygroupids select u;     list<selectlistitem> lst = new list<selectlistitem>();     foreach (var u in unit)     {         lst.add(new selectlistitem { text = u.activitygroupname.tostring(), value = u.activitygroupid_fk.tostring() });     }     viewdata["activity"] = lst;     return view(); }  [httppost] public actionresult createactivity(formcollection model) {          if (modelstate.isvalid)         {             activity activ = new activity();                 if (!string.isnullorempty(model["activitygroupid_fk"]))                 {                     ariacrmentities aria = new ariacrmentities();                     activ.activitygroupid_fk = int32.parse(model["activitygroupid_fk"]);                     activ.activitytype = model["activitytype"];                     activ.activitydes = model["activitydes"];                     aria.activities.addobject(activ);                     aria.savechanges();                     return redirecttoaction("create");                 }         }         return view(model); } 

this view :

<% using (html.beginform()) {%> <%: html.validationsummary(true,) %> <fieldset>     <legend>fields</legend> <br /> <%:html.dropdownlistfor(model=>model.activitygroupid_fk , (ienumerable<selectlistitem>)viewdata["activity"], "انتخاب نوع فعالیت")%><br />  <%html.validationmessagefor (model=>model.activitygroupid_fk,"dddddddddd"); %>     <div class="editor-label">         <%: html.labelfor(model => model.activitytype) %>     </div>     <div class="editor-field">         <%: html.textboxfor(model => model.activitytype) %>         <%: html.validationmessagefor(model => model.activitytype,"dfsaaa") %>     </div>     <div class="editor-label">         <%: html.labelfor(model => model.activitydes) %>     </div>     <div class="editor-field">         <%: html.textboxfor(model => model.activitydes) %>         <%: html.validationmessagefor(model => model.activitydes) %>     </div>     <p>         <input type="submit" value="create" id="btn"/>     </p> </fieldset> <% } %> 

but need validate <%:html.dropdownlistfor%>. how create custom validation?

if want display validation error message next drop-down list, can controller so:

modelstate.addmodelerror("activitygroupid_fk", "the selected value invalid."); 

update

i noticed validation message dropdownlist looks this:

<%html.validationmessagefor (model=>model.activitygroupid_fk,"dddddddddd"); %> 

you might want change so:

<%: html.validationmessagefor(model=>model.activitygroupid_fk,"dddddddddd") %> 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -