【问题标题】:TextBoxFor value not passed to model when disabled禁用时 TextBoxFor 值未传递给模型
【发布时间】:2016-01-26 20:50:48
【问题描述】:

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

<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 数据为空,如果我删除它,我会得到正确的数据。

我做错了什么?

【问题讨论】:

标签: c# asp.net-mvc razor


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您需要在与禁用字段匹配的页面上添加&lt;input type="hidden" name="whateverName" /&gt;。默认不会发送到服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多