MVC2 Controller is passed a null object as a parameter

Posted by Steve Wright on Stack Overflow See other posts from Stack Overflow or by Steve Wright
Published on 2010-03-16T04:30:03Z Indexed on 2010/03/17 2:11 UTC
Read the original article Hit count: 702

Filed under:

I am having an issue with a controller getting a null object as a parameter:

[HttpGet]
public ActionResult Login()
{
    return View();
}

[HttpPost]
public ActionResult Login(LoginViewData userLogin)
{
    Assert.IsNotNull(userLogin); // FAILS
    if (ModelState.IsValid)
    {

    }
    return View(userLogin);
}

The LoginViewData is being passed as null when the HttpPost is called: Using MvcContrib.FluentHtml:

<h2>Login to your Account</h2>
<div id="contact" class="rounded-10">
<%using (Html.BeginForm()) { %>
    <fieldset>
        <ol>
            <li>
                <%= this.TextBox(f=>f.UserLogin).Label("Name: ", "name") %>                                        
                <%= Html.ValidationMessageFor(m => m.UserLogin) %>
            </li>
            <li>                    
                <%= this.Password(u => u.UserPassword).Label("Password:", "name") %>                                        
                <%= Html.ValidationMessageFor(m => m.UserPassword) %>
            </li>
            <li>
                <%= this.CheckBox(f => f.RememberMe).LabelAfter("Remember Me")%>                    
            </li>
            <li>                    
                <label for="submit" class="name">&nbsp;</label>
                <%= this.SubmitButton("Login")%>
            </li>
        </ol>
    </fieldset>
<% } %>
<p>If you forgot your user name or password, please use the Password Retrieval Form.</p>
</div>

The view inherits from MvcContrib.FluentHtml.ModelViewPage and is strongly typed against the LoginViewData object:

public class LoginViewData 
{
    [Required]
    [DisplayName("User Login")]
    public string UserLogin { get; set; }
    [Required]
    [DisplayName("Password")]
    public string UserPassword { get; set; }
    [DisplayName("Remember Me?")]
    public bool RememberMe { get; set; }
}

Any ideas on why this would be happening?

UPDATE

I rebuilt the web project from scratch and that fixed it. I am still concerned why it happened.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2