【问题标题】:Html.TextBox conditional attribute with ASP.NET MVC Preview 5带有 ASP.NET MVC Preview 5 的 Html.TextBox 条件属性
【发布时间】:2010-09-15 17:09:00
【问题描述】:

我有一个强类型的 MVC 视图控件,它负责用户可以创建和编辑客户端项目的 UI。我希望他们能够在创建时定义 ClientId,但不能编辑,这会反映在 UI 中。

为此,我有以下几行:

<%= Html.TextBox("Client.ClientId", ViewData.Model.ClientId, new 
 { @readonly = 
   (ViewData.Model.ClientId != null && ViewData.Model.ClientId.Length > 0 
      ? "readonly" : "false") 
 } )
%>

似乎无论我给 readonly 属性赋予什么值(甚至是“false”和“”),Firefox 和 IE7 都会将输入设为只读,这非常违反直觉。如果不需要,是否有一种很好的、​​基于三元运算符的方法来完全删除属性?

【问题讨论】:

    标签: asp.net-mvc attributes html-helper readonly


    【解决方案1】:

    我用这个:

       @Html.TextAreaFor(model => model.ComentarioGestor, comentarioGestor? new { @class = "form-control" } : new { @class = "form-control", @readonly = "readonly" } as object)
    

    【讨论】:

      【解决方案2】:

      提示:仅存在 readonly/disabled 属性会使元素在浏览器中只读或禁用。

      @Html.TextBoxFor(x => x.Name, isReadonly ?(object) new { @readonly = true } : new { /*Some other attributes*/ })
      

      【讨论】:

      • 为什么三元运算的第一个条件需要将匿名类强制转换为对象?不挑战它,只是试图理解它。谢谢!
      【解决方案3】:

      我尝试了上面的大部分建议,现在我只用一行就得出了最简单的建议。通过将其中一个声明为“对象”类型来组合 2 个匿名 html 属性对象。

      @Html.TextBoxFor(m => m.Email, !isEdit ? new { id = "email_box" } : new { id = "email_box", @readonly = isEdit ? "readonly" : "false" } as object)
      

      【讨论】:

      • 这对我不起作用; @readonly = false 仍被解释为 readonly
      【解决方案4】:

      如果你想定义几个属性,并且条件只读不重复其他属性, 您可以对属性使用 Dictionary 而不是匿名类型。

      例如

      Dictionary<string, object> htmlAttributes = new Dictionary<string, object>();
      htmlAttributes.Add("class", "myCSS");
      htmlAttributes.Add("data-attr1", "val1");
      htmlAttributes.Add("data-attr2", "val2");
      if (Model.LoggedInData.IsAdmin == false)
      {
          htmlAttributes.Add("readonly", "readonly");
      }
      
      
      @:User: @Html.TextBoxFor(
          m => m.User,
          htmlAttributes)  
      

      【讨论】:

      • 应该是答案,因为它避免了重复。
      【解决方案5】:
      $(函数() { $("[readonly='false']").removeAttr("readonly"); });

      【讨论】:

        【解决方案6】:

        我觉得应该是

        <%= ((bool) Eval("InRole")) ? "checked" : "" %> 
        

        而不是这个在 leppies 答案中。

        <%# ((bool) Eval("InRole")) ? "checked" : "" %> 
        

        至少它不适用于我,但它适用于 =。我做错什么了吗?无论如何感谢您的提示:)

        【讨论】:

          【解决方案7】:

          另一种方法是将其作为普通的旧 HTML 发出。是的,编辑器会让你认为你错了,但是这似乎在 VS2008SP1 中经常发生。此示例专门针对在 CTP5 中似乎完全被浪费的复选框,但它让您了解如何发出条件属性。

          <input type="checkbox" name="roles" value='<%# Eval("Name") %>' 
            <%# ((bool) Eval("InRole")) ? "checked" : "" %> 
            <%# ViewData.Model.IsInRole("Admin") ? "" : "disabled" %> />
          

          【讨论】:

            【解决方案8】:

            棘手的问题...但是,如果您只想定义readonly 属性,您可以这样做:

            <%= Html.TextBox("Client.ClientId", ViewData.Model.ClientId, 
              ViewData.Model.ClientId != null && ViewData.Model.ClientId.Length > 0 
                ? new { @readonly =  "readonly" } 
                : null) 
            %>
            

            如果要定义更多属性,则必须定义两个匿名类型并拥有多个属性副本。例如,像这样的东西(反正我不喜欢):

            ClientId.Length > 0 
              ? (object)new { @readonly = "readonly", @class = "myCSS" } 
              : (object)new { @class = "myCSS" }
            

            【讨论】:

            • +1 我的第一个镜头是第二个代码 sn-p,没有强制转换为对象。它很容易被忽视,但非常必要。
            猜你喜欢
            • 2011-03-01
            • 2010-09-11
            • 1970-01-01
            • 2010-09-08
            • 1970-01-01
            • 2012-03-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多