2

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]}

4
  • And I assume you checked that the correct action is hit after posting the form? Commented Apr 23, 2009 at 3:51
  • Yes, the correct action is getting called after posting the form. Commented Apr 23, 2009 at 12:32
  • It's weird. I have the exact code working in front of me. Are you using a different model binder maybe? Commented Apr 23, 2009 at 13:13
  • very odd, I have run into a similar problem as well and no idea what is causing it. Same thing with the value [public ActionResult Create(Text value)] being null except a similer action for another controller works fine. Nothing special in that class except for 1 Guid property and 2 string properties. Commented Apr 24, 2009 at 5:48

5 Answers 5

1

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?            
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

0

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

Comments

0

Please add name property to your control (Html.TextBox("UserName"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.