模型绑定禁用了文本框 [英] Model Binding With Disabled Textbox

查看:107
本文介绍了模型绑定禁用了文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我定义为

<%= Html.TextBox("Username", Model.Form.Username, 
        new { @class = "textbox", @disabled = "disabled" })%>

在我的行动

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateAntiForgeryToken]
    public ActionResult EditLogin(LoginForm post) {

        ...

        return View(model);
    }

post.Username将是空白,所有其他属性绑定正确的,但如果我改变@禁用=残疾人@只读=只读的用户名正确结合,一切工作。

post.Username will be blank, all other properties bind correctly, but if I change @disabled="disabled to @readonly="readonly" the username binds properly and everything works.

它看起来像模型绑定在残疾人领域的忽略值。难道自己解决的办法?我还需要该字段的值绑定到模型。我可以只读使用,但将preFER使用禁用所以它是明显可见的用户,他们不能编辑该字段的值。

It looks like model binding ignores values in disabled fields. Is their a way around this? I still need the field's value to bind to the model. I can use readonly but would prefer to use disabled so it is visually apparent to the user that they cannot edit the value of the field.

推荐答案

我相信一个表单字段被禁用不提交任何东西。如果你有一个表单,并以该形式禁用foo的领域,当你发布后不会对foo的字段的值。这是一个简单的HTML禁用场的性质,而不是一个MVC问题

I believe a form field that is disabled does not submit anything. If you have a form and disable the foo field in that form, when you post the post will not have the value for the foo field. This is simply the nature of disabling a field in HTML and is not a MVC issue.

这篇关于模型绑定禁用了文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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