asp.net - My form ignores Required attributes from model -


i'm having troubles form validation. reason form ignores [required] attribute have in model.

model: (stripped down easy read)

public class notemployeesmodel {     public notemployeesmodel()     {         detailmodel = new notemployeesdetailmodel();     }     public notemployeesdetailmodel detailmodel { get; set; } }  public class notemployeesdetailmodel {     public notemployeesdetailmodel()     {      }      public notemployeesdocumentmodel documentmodel { get; set; } }  public class notemployeesdocumentmodel {     public notemployeesdocumentmodel()     {      }     public notemployeedocumentinputmodel documentinput { get; set; }      public class notemployeedocumentinputmodel     {         public notemployeedocumentinputmodel()         {          }          public notemployeedocumentinputmodel(int notemployeeid)         {             notemployeeid = notemployeeid;         }          public int notemployeeid { get; set; }         public int selecteddocumenttype { get; set; }          [required(errormessageresourcetype = typeof(errormessages), errormessageresourcename = "star")]         public string description { get; set; }          [required(errormessageresourcetype = typeof(errormessages), errormessageresourcename = "star")]         public httppostedfilebase file { get; set; }     } } 

for each view or partial have separate model class.

form:

@model notemployeesdocumentmodel  @using (html.beginform("addnotemployeedocument", "home", formmethod.post, new { id = "form-add-document", enctype = "multipart/form-data" })) { @html.hiddenfor(x => x.documentinput.notemployeeid) <table class="table-output">     <thead>         <tr>             <td>@html.label("sort", labels.sort)</td>             <td>@html.label("description", labels.description)</td>             <td>@html.label("type", labels.type)</td>             <td class="text-align-right"></td>         </tr>     </thead>     <tbody>             <tr>                 <td>                     <i class="fa fa-plus-square cursor-pointer add-document"></i>                     <input type="file" id="documentinput_file" name="documentinput.file" required="required" />                     @html.validationmessagefor(x => x.documentinput.file)                 </td>                 <td>                     @html.textboxfor(x => x.documentinput.description, new { @class = "width300px hardware-setup-input", placeholder = "vul hier een omschrijving in..." })                     @html.validationmessagefor(x => x.documentinput.description)                 </td>                 <td>                     @html.dropdownlistfor(x => x.documentinput.selecteddocumenttype, model.documenttypes, "--- maak een keuze ---")                 </td>                 <td class="text-align-right">                     <span id="btn-add-document" class="button-org">@labels.save</span>                 </td>             </tr>      </tbody> </table> } 

structure of page: view / partial / partial (here form)

js:

$("#btn-add-document").on("click", function () {             var frm = $("#form-add-document");             if (frm.valid()) {                 frm.ajaxsubmit({                     datatype: "html",                     success: function (responsetext) {                         $("#document-container").html(responsetext);                         $("#documentinput_description").text("");                         $("#documentinput_selecteddocumenttype").val("");                         loaddocumentpartial();                     }                 });             }         }); 

i'm using jquery form plugin malsup submit form through ajax.

controller:

[httppost] public actionresult addnotemployeedocument(notemployeesdocumentmodel input) {     // code  } 

as can see in parameters of actionresult can't put notemployeesdocumentinputmodel i'm forced use parent class.

i have no idea i'm doing wrong. first time encounter such problem.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -