Asp.net mvc ASP.NETMVC模型绑定返回空值
给定以下标记:Asp.net mvc ASP.NETMVC模型绑定返回空值,asp.net-mvc,Asp.net Mvc,给定以下标记: <form method="post" action="/home/index"> Username: <%= Html.TextBox("UserName")%> Password: <%= Html.TextBox("Password")%> <input id="login" type="button" value="Login" /> <input id="Submit
<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>
编辑:
表单值正在发布。如果我检查Request.Form属性,我会看到正在发布正确的值
??申请表格
{UserName=sdf&Password=sdf}
[System.Web.HttpValueCollection]:{UserName=sdf&Password=sdf}
base{System.Collections.Specialized.NameObjectCollectionBase}:{UserName=sdf&Password=sdf}
所有键:{string[2]}
Steve我也遇到了类似的问题,我发现这是因为我的模型上有属性键和值,而模型绑定器不喜欢这些属性
尝试将用户名更改为user,密码更改为pass,然后查看问题是否仍然存在。运行此操作时,您可以尝试将调试器附加到MVC源代码,以查看引擎盖/引擎盖下的情况。是否尝试将Bind属性添加到参数中
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index([Bind]string UserName, [Bind]string Password)
{
//UserName and Password are null! Why?
}
我有一个类似的问题,结果是由于字段的命名
<form method="post" action="/company/update/3">
Name:
<%= Html.TextBox("Company.Name")%>
FEIN:
<%= Html.TextBox("FEIN")%>
<input type="submit" value="submit" />
</form>
如果Company.Name是发布的第一个值,则可能会发生这种情况。请将Name属性添加到控件(Html.TextBox(“用户名”),并且我假设您在发布表单后检查了是否命中了正确的操作?是的,在发布表单后调用了正确的操作。这很奇怪。我的面前有准确的代码。您是否使用了不同的活页夹?非常奇怪,我也遇到了类似的问题,不知道是什么原因造成的。值[public ActionResult Create(Text value)]为null时也是如此,但另一个控制器的类似操作可以正常工作。除1个Guid属性和2个字符串属性外,该类中没有任何特殊内容。
<form method="post" action="/company/update/3">
Name:
<%= Html.TextBox("Company.Name")%>
FEIN:
<%= Html.TextBox("FEIN")%>
<input type="submit" value="submit" />
</form>
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int id, Company company)
{
//Company.FEIN is null!
}