asp.net mvc - Model is NULL in Ajax POST to controller action -
i have simple model , view. though, modelbinder seems fail when trying bind model because receive null controller action. doing wrong?
razor code:
@model bikesharing.views.shared.widgets.popups.logininputmodel @using (ajax.beginform("login",null, new ajaxoptions { updatetargetid = "login-partial-update", httpmethod = "post" }, new { id = "js-form-login" })) { @html.textboxfor(x => x.email, new {placeholder = "email address"}) <div class="errormessage"> @html.validationmessagefor(x=>x.email) </div> @html.passwordfor(x => x.password, new {placeholder = "password"}) <div class="errormessage"> @html.validationmessagefor(x => x.password) </div> }
controller action:
[httppost] public actionresult login(logininputmodel lmod) { if (modelstate.isvalid) { // code never reached because lmod null } return partialview("widgets/popups/_logininput", lmod); }
model code:
public class logininputmodel { [required(errormessage = "your email address required.")] [emailaddress] public string email { get; private set; } [required(errormessage = "please provide password.")] [minlength(6,errormessage = "your password short.")] [maxlength(50, errormessage = "your password long.")] public string password { get; private set; } public logininputmodel() { } public logininputmodel(string email, string password) { email = email; password = password; } }
the form submit done via jquery-unobtrusive-ajax , ajax.beginform()
firing via $('#js-form-login').submit();
rendered html in browser:
<form action="/home/login" data-ajax="true" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#login-partial-update" id="js-form-login" method="post" novalidate="novalidate"> <input data-val="true" data-val-email="the email field not valid e-mail address." data-val-required="your email address required." id="email" name="email" placeholder="email address" type="text" value=""> <div class="errormessage"> <span class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace="true"></span> </div> <input data-val="true" data-val-maxlength="your password long." data-val-maxlength-max="50" data-val-minlength="your password short." data-val-minlength-min="6" data-val-required="please provide password." id="password" name="password" placeholder="password" type="password"> <div class="errormessage"> <span class="field-validation-valid" data-valmsg-for="password" data-valmsg-replace="true"></span> </div> </form>
try adding formbody
prefix hint modelbinder in post body.
[httppost] public actionresult login([frombody]logininputmodel lmod) { if (modelstate.isvalid) { // code never reached because lmod null } return partialview("widgets/popups/_logininput", lmod); }
Comments
Post a Comment