Username: <%= Html.TextBox("UserN...">

ASP.Net MVC Binding Returning Null Values

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 model binding does not work when my action is called:

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

Edit: Form values ​​are published. If I check the Request.Form property, I see that the correct values ​​are published.

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

+2
source share
5 answers

Have you tried to add a parameter binding attribute?

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

I had a similar problem, and this turned out to be related to the field names.

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

When sent to:

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

This is similar to the first post posted in Company.Name.

+1
source

I had a similar problem, and I found that this was due to the fact that I had the Key and Value properties on my model, which did not like the model binder.

Try changing the UserName to the user and password for the transfer and see if there is a problem.

0
source

You can try connecting the debugger to the MVC source when it starts to see what happens under the hood / hood.

0
source

Add the name property (Html.TextBox ("UserName")) to your element

0
source

All Articles