TextBoxFor值在禁用时未传递给模型

时间:2015-10-27 12:29:21

标签: c# asp.net-mvc razor

我有一个禁用的输入字段显示模型中的数据。

<div class="form-group">
                    @Html.LabelFor(model => model.first_name, "Förnamn", new
            {
                @for = "inputFirstname",
                @class = "col-lg-3 control-label"
            })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(model => model.first_name, new
                {
                    @type = "text",
                    @class = "form-control",
                    @id = "inputFirstname",
                    text = Html.DisplayNameFor(model => model.first_name),
                    disabled="disabled"
                })
                    </div>
                </div>

我可以将这些数据提交给一个控制方法:

[HttpPost]

public ActionResult Index(RegistrationModel RegistrationModelViewModel)
{}

当我添加disabled="disabled"时,first_name数据为空,如果我删除它,我会得到正确的数据。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要在与禁用字段匹配的页面上添加<input type="hidden" name="whateverName" />。默认情况下,它不会被发送到服务器。

答案 1 :(得分:2)

如果您还要显示数据,可能需要使用 readonly 属性:

@Html.TextBoxFor(model => model.first_name, new
    {
        @readonly = "readonly"
    })