asp.net-mvc 带有 ASP.NET MVC Preview 5 的 Html.TextBox 条件属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/177673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html.TextBox conditional attribute with ASP.NET MVC Preview 5
提问by tags2k
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 ClientIdon creation, but not edit, and this to be reflected in the UI.
我有一个强类型的 MVC 视图控件,它负责用户可以创建和编辑客户端项目的 UI。我希望他们能够ClientId在创建时定义,但不能编辑,这将反映在 UI 中。
To this end, I have the following line:
为此,我有以下几行:
<%= Html.TextBox("Client.ClientId", ViewData.Model.ClientId, new
{ @readonly =
(ViewData.Model.ClientId != null && ViewData.Model.ClientId.Length > 0
? "readonly" : "false")
} )
%>
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?
似乎无论我给 readonly 属性赋予什么值(甚至“false”和“”),Firefox 和 IE7 都会将输入设为只读,这令人讨厌地违反直觉。如果不需要,是否有一种不错的基于三元运算符的方法可以完全删除该属性?
回答by Panos
Tough problem... However, if you want to define only the readonlyattribute, you can do it like this:
棘手的问题......但是,如果你只想定义readonly属性,你可以这样做:
<%= 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" }
回答by Aviko
If you want to define several attributes, and conditional readonly without duplicate the other attributes, you can use Dictionary instead of anonymous types for the attributes.
如果您想定义多个属性,并且条件只读而不重复其他属性,您可以使用 Dictionary 而不是属性的匿名类型。
e.g.
例如
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)
回答by pranavn
Tip: Its the mere presence of readonly/disabled attribute that makes the element readonly or disabled in the browser.
提示:仅存在 readonly/disabled 属性可使元素在浏览器中只读或禁用。
@Html.TextBoxFor(x => x.Name, isReadonly ?(object) new { @readonly = true } : new { /*Some other attributes*/ })
回答by leppie
And alternative is just to emit it as plain old HTML. Yes, the editor will make you think you are wrong, but that seems to happen quite frequently with VS2008SP1. This example is specifically for checkboxes which seems to be completely wasted in CTP5, but it gives you an idea how to emit conditional attributes.
另一种方法是将它作为普通的旧 HTML 发出。是的,编辑器会让你认为你错了,但在 VS2008SP1 中这种情况似乎经常发生。这个例子专门针对在 CTP5 中似乎完全浪费的复选框,但它让您了解如何发出条件属性。
<input type="checkbox" name="roles" value='<%# Eval("Name") %>'
<%# ((bool) Eval("InRole")) ? "checked" : "" %>
<%# ViewData.Model.IsInRole("Admin") ? "" : "disabled" %> />
回答by Olaj
I think it should be
我认为应该是
<%= ((bool) Eval("InRole")) ? "checked" : "" %>
instead of this in leppies answer.
而不是在leppies回答中。
<%# ((bool) Eval("InRole")) ? "checked" : "" %>
At least it did not work for me with # but it worked with =. Did i do anything wrong? Thanks for the tip anyway :)
至少它对我不起作用#但它与=一起工作。我做错了什么吗?无论如何,感谢您的提示:)
回答by cristian gonzalez
i use this :
我用这个:
@Html.TextAreaFor(model => model.ComentarioGestor, comentarioGestor? new { @class = "form-control" } : new { @class = "form-control", @readonly = "readonly" } as object)
回答by silversumo
回答by cronixis
I tried most of the suggestions above and now I have arrived at the simplest with a single line. Combine 2 anonymous html attributes object by declaring wither one of it as "object" type.
我尝试了上面的大部分建议,现在我得到了最简单的一行。通过将其中一个声明为“对象”类型来组合 2 个匿名 html 属性对象。
@Html.TextBoxFor(m => m.Email, !isEdit ? new { id = "email_box" } : new { id = "email_box", @readonly = isEdit ? "readonly" : "false" } as object)

