在ASP.NET MVC preVIEW 5 Html.TextBox条件属性 [英] Html.TextBox conditional attribute with ASP.NET MVC Preview 5

查看:110
本文介绍了在ASP.NET MVC preVIEW 5 Html.TextBox条件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个强类型MVC视图控件负责用户可以创建和编辑客户端项目的用户界面。我希望他们能够定义客户端Id 创作上,但不能编辑,这在用户界面中反映出来。

I have a strongly-typed MVC View Control which is responsible for the UI where users can create and edit Client items. I'd like them to be able to define the ClientId on creation, but not edit, and this to be reflected in the UI.

为此,我有以下行:

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

看来,无论什么价值我给只读属性(甚至是假和),Firefox和IE7使输入只读,这是烦人反直觉。是否有一个很好的,三元运营商为主的方式完全掉落,如果不是必需的属性?

It seems that no matter what value I give the readonly attribute (even "false" and ""), Firefox and IE7 make the input read-only, which is annoyingly counter-intuitive. Is there a nice, ternary-operator-based way to drop the attribute completely if it is not required?

推荐答案

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

Tough problem... However, if you want to define only the readonly attribute, you can do it like this:

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

如果要定义更多的属性,那么你必须定义两个匿名类型和具有的属性的多个副本。例如,像这样(我不喜欢的反正):

If you want to define more attributes then you must define two anonymous types and have multiple copies of the attributes. For example, something like this (which I don't like anyway):

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

这篇关于在ASP.NET MVC preVIEW 5 Html.TextBox条件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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