asp.net mvc 3模型绑定器不绑定

发布于 2024-12-19 23:28:07 字数 1650 浏览 1 评论 0原文

我有一个登录页面,似乎登录信息没有绑定到我的模型,

@using (Html.BeginForm("LogOn", "Account"))
{
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.Password)
            </div>
            <div class="editor-label">
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe)
            </div>
            <p>
                <input type="submit" value="Log On" />
            </p>
        </fieldset>
    </div>
}
...
 [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            Log(new Exception(string.Format("model username : {0}, password : {1}, request[username] {2} , request[password] : {3}", model.UserName, model.Password, Request["UserName"], Request["Password"])));
            try
            {...

模型信息为空,即使在 firebug 中我可以看到表单已发布

,这只发生在我的产品服务器上,

一次在 本地工作正常过一会儿它就会工作并让您登录

I have a logon page and it seem like the login info is not being bound to my model

@using (Html.BeginForm("LogOn", "Account"))
{
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password, new { style = " width:200px" })
                @Html.ValidationMessageFor(m => m.Password)
            </div>
            <div class="editor-label">
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe)
            </div>
            <p>
                <input type="submit" value="Log On" />
            </p>
        </fieldset>
    </div>
}
...
 [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            Log(new Exception(string.Format("model username : {0}, password : {1}, request[username] {2} , request[password] : {3}", model.UserName, model.Password, Request["UserName"], Request["Password"])));
            try
            {...

the model info is empty, even though in firebug i can see that the form was posted

this only happens on my prod server, works fine locally

once in a while it will work and let you log in

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熟人话多 2024-12-26 23:28:07

可能 string returnUrl 是令人困惑的绑定器,我认为在您的示例中,您通常需要使用绑定前缀作为模型名称,但它可以是任何字符串,因此您的表单将使用绑定元素名称呈现类似

<input type='text' name='your_prefix[RememberMe]' id="your_prefix_RememberMe" />
...

,稍后您可以通过添加操作(方法)参数 BindAttribute 来获取它,如下所示

[HttpPost]
public ActionResult LogOn([Bind(Prefix = "your_prefix")] LogOnModel model, string returnUrl){
  ...
}

Probably string returnUrl is confusing binder, I think in your exapmple you will need to use binding prefix, usually, as model name, but It can be any string, so your form will be rendered with binding element names like

<input type='text' name='your_prefix[RememberMe]' id="your_prefix_RememberMe" />
...

and later you can fetch it by adding in your action (method) parameter BindAttribute like folowing

[HttpPost]
public ActionResult LogOn([Bind(Prefix = "your_prefix")] LogOnModel model, string returnUrl){
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文