ASP.Net MVC模型绑定返回空值 [英] ASP.Net MVC Model Binding Returning Null Values

查看:63
本文介绍了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="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?            
    }

表单值被发布.如果检查Request.Form属性,则会看到发布了正确的值.

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

?申请表{UserName = sdf& Password = sdf}[System.Web.HttpValueCollection]:{UserName = sdf& Password = sdf}基本{System.Collections.Specialized.NameObjectCollectionBase}:{UserName = sdf& Password = sdf}AllKeys:{string [2]}

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

推荐答案

我有一个类似的问题,事实证明这是由于字段的命名引起的.

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>

发布到:

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

如果Company.Name是发布的第一个值,这似乎会发生.

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

这篇关于ASP.Net MVC模型绑定返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆