【问题标题】:How to create a readonly textbox in ASP.NET MVC3 Razor如何在 ASP.NET MVC3 Razor 中创建只读文本框
【发布时间】:2012-02-04 09:46:48
【问题描述】:

如何使用 Razor 视图引擎在 ASP.NET MVC3 中创建只读文本框?

是否有可用的 HTMLHelper 方法来执行此操作?

类似以下内容?

@Html.ReadOnlyTextBoxFor(m => m.userCode)

【问题讨论】:

    标签: c# asp.net-mvc-3 razor html-helper readonly-attribute


    【解决方案1】:
    @Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" })
    

    欢迎您为此创建一个 HTML Helper,但这只是一个 HTML 属性,就像其他任何属性一样。您会为具有其他属性的文本框制作 HTML 助手吗?

    【讨论】:

    • @Shyju 对不起,我错过了readonly 属性的@ 前缀。查看我的编辑。
    • 以后,如果您在将属性添加到动态对象参数时遇到任何类型的错误,您可以在它们前面加上 @。您通常只会看到与 HTML 属性匹配的关键字(如只读、类等)
    • @BradChristie:不;您只需要 @ 即可使用匹配 C# 关键字的属性。
    • @BradChristie:我误读了您的评论(“关键字”含糊不清)
    • 如果你有多个 html 属性,你可以这样做:@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly", @class="form-control" })
    【解决方案2】:

    更新: 现在,将 HTML 属性添加到默认编辑器模板非常简单。它不是这样做:

    @Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" })
    

    你可以这样做:

    @Html.EditorFor(m => m.userCode, new { htmlAttributes = new { @readonly="readonly" } })
    

    优点:您不必致电.TextBoxFor 等获取模板。只需致电.EditorFor


    虽然@Shark 的解决方案工作正常,而且简单实用,但我的解决方案(我一直使用的)是这样的:创建一个可以处理readonly 属性的editor-template

    1. ~/Views/Shared/ 中创建一个名为EditorTemplates 的文件夹
    2. 创建剃须刀PartialView,命名为String.cshtml
    3. 用此代码填写String.cshtml

      @if(ViewData.ModelMetadata.IsReadOnly) {
          @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
              new { @class = "text-box single-line readonly", @readonly = "readonly", disabled = "disabled" })
      } else {
          @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
              new { @class = "text-box single-line" })
      }
      
    4. 在模型类中,将[ReadOnly(true)] 属性放在您想成为readonly 的属性上。

    例如,

    public class Model {
        // [your-annotations-here]
        public string EditablePropertyExample { get; set; }
    
        // [your-annotations-here]
        [ReadOnly(true)]
        public string ReadOnlyPropertyExample { get; set; }
    }
    

    现在您可以简单地使用 Razor 的默认语法:

    @Html.EditorFor(m => m.EditablePropertyExample)
    @Html.EditorFor(m => m.ReadOnlyPropertyExample)
    

    第一个渲染一个普通的text-box,像这样:

    <input class="text-box single-line" id="field-id" name="field-name" />
    

    第二个会渲染到;

    <input readonly="readonly" disabled="disabled" class="text-box single-line readonly" id="field-id" name="field-name" />
    

    您可以将此解决方案用于任何类型的数据(DateTimeDateTimeOffsetDataType.TextDataType.MultilineText 等)。只需创建一个editor-template

    【讨论】:

    • +1 因为您使用了“ViewData.ModelMetadata.IsReadOnly”。我期待 MVC 会在版本 4 中考虑这些内容。
    • @cleftheris 好吧,我们现在是第 5 版,而 MVC 仍然没有接受它们 ;)
    • @Javad_Amiry - 很好的答案 - 我已经实现了它,它似乎工作得很好,直到我点击了脚手架编辑页面上的保存。然后事实证明带有 [ReadOnly(true)] 的属性将导致 NULL 被发送到数据库而不是真正的属性值 - 你遇到过这个吗?
    【解决方案3】:

    TextBoxFor 的解决方案是可以的,但是如果你不想看到像 EditBox 这样时髦的字段(这可能会让用户有点困惑)涉及如下更改:

    1. 修改前的剃刀代码

      <div class="editor-field">
           @Html.EditorFor(model => model.Text)
           @Html.ValidationMessageFor(model => model.Text)
      </div>
      
    2. 修改后

      <!-- New div display-field (after div editor-label) -->
      <div class="display-field">
          @Html.DisplayFor(model => model.Text)
      </div>
      
      <div class="editor-field">
          <!-- change to HiddenFor in existing div editor-field -->
          @Html.HiddenFor(model => model.Text)
          @Html.ValidationMessageFor(model => model.Text)
      </div>
      

    通常,此解决方案会阻止编辑字段,但会显示其价值。 无需进行代码隐藏修改。

    【讨论】:

    • 我认为在这里为类 editor-field 和 display-field 设置 CSS 可能真的很有帮助。
    • 你说的“这个解决方案禁用归档反对编辑”是什么意思,(似乎难以理解)?请通过editing your answer 回复,而不是在 cmets 中(视情况而定)。
    【解决方案4】:

    感谢@Bronek 和@Shimmy 之前的回答:

    这就像我在 ASP.NET Core 中做过同样的事情:

    <input asp-for="DisabledField" disabled="disabled" />
    <input asp-for="DisabledField" class="hidden" />
    

    第一个输入是只读的,第二个将值传递给控制器​​并隐藏。我希望它对使用 ASP.NET Core 的人有用。

    【讨论】:

      【解决方案5】:
       @Html.TextBox("Receivers", Model, new { @class = "form-control", style = "width: 300px", @readonly = "readonly" })
      

      【讨论】:

      • 解释一下。
      【解决方案6】:
      @Html.TextBoxFor(model => model.IsActive, new { readonly= "readonly" })
      

      这对于文本框来说很好。但是,如果您尝试对 checkbox 执行相同操作,那么如果您正在使用它,请尝试使用它:

      @Html.CheckBoxFor(model => model.IsActive, new { onclick = "return false" })
      

      但不要使用disable,因为禁用总是将默认值false 发送到服务器——它处于选中或未选中状态。而readonly 不适用于复选框和radio buttonreadonly 仅适用于 text 字段。

      【讨论】:

      【解决方案7】:

      您可以使用以下代码将 TextBox 创建为只读。

      方法一

       @Html.TextBoxFor(model => model.Fields[i].TheField, new { @readonly = true })
      

      方法二

      @Html.TextBoxFor(model => model.Fields[i].TheField, new { htmlAttributes = new {disabled = "disabled"}})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多