freddoo
freddoo

Reputation: 6858

asp.net mvc 3 model binder not binding

I have a logon page and it seem like the login info is not being bound to my model

@using (Html.BeginForm("LogOn", "Account"))
{
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.Password)
            </div>
            <div class="editor-label">
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe)
            </div>
            <p>
                <input type="submit" value="Log On" />
            </p>
        </fieldset>
    </div>
}
...
 [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            Log(new Exception(string.Format("model username : {0}, password : {1}, request[username] {2} , request[password] : {3}", model.UserName, model.Password, Request["UserName"], Request["Password"])));
            try
            {...

the model info is empty, even though in firebug i can see that the form was posted

this only happens on my prod server, works fine locally

once in a while it will work and let you log in

Upvotes: 1

Views: 1329

Answers (1)

Milan Jaric
Milan Jaric

Reputation: 5646

Probably string returnUrl is confusing binder, I think in your exapmple you will need to use binding prefix, usually, as model name, but It can be any string, so your form will be rendered with binding element names like

<input type='text' name='your_prefix[RememberMe]' id="your_prefix_RememberMe" />
...

and later you can fetch it by adding in your action (method) parameter BindAttribute like folowing

[HttpPost]
public ActionResult LogOn([Bind(Prefix = "your_prefix")] LogOnModel model, string returnUrl){
  ...
}

Upvotes: 1

Related Questions