tags:

views:

840

answers:

4

Given the following markup:

 <form method="post" action="/home/index">
    Username:
    <%= Html.TextBox("UserName")%>
    Password:
    <%= Html.TextBox("Password")%>
    <input id="login" type="button" value="Login" />
    <input id="Submit1" type="submit" value="submit" />
 </form>

Can you tell me why the model binding is not working when invoking my action:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(string UserName, string Password)
    {
        //UserName and Password are null!  Why?            
    }

Edit: The form values are getting posted. If I inspect the Request.Form property, I see that the correct values are being posted.

? Request.Form {UserName=sdf&Password=sdf} [System.Web.HttpValueCollection]: {UserName=sdf&Password=sdf} base {System.Collections.Specialized.NameObjectCollectionBase}: {UserName=sdf&Password=sdf} AllKeys: {string[2]}

A: 

Steve I was having a similar problem and I found it to be because I had the properties Key and Value on my model which the model binder does not like.

Try changing the UserName to user and Password to pass and see if the problem still exists.

Mike Geise
A: 

You could try attaching the debugger to the MVC source when you run this, to see what's going on under the bonnet/hood.

UpTheCreek
A: 

Did you try adding the Bind Attribute to the parameters?

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index([Bind]string UserName, [Bind]string Password)
{
    //UserName and Password are null!  Why?            
}
Chuck Conway
A: 

I had a similar problem, and it turned out to be due to the naming of the fields.

<form method="post" action="/company/update/3">
   Name:
   <%= Html.TextBox("Company.Name")%>
   FEIN:
   <%= Html.TextBox("FEIN")%>

   <input type="submit" value="submit" />
</form>

When posted to:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int id, Company company)
{
    //Company.FEIN is null!
}

This seems to happen if Company.Name is the first value posted.

Lance Fisher