MVC 4剃刀数据注释只读 [英] MVC 4 razor Data Annotations ReadOnly
问题描述
只读属性似乎并没有在MVC 4.编辑(假)的属性不工作,我希望它的方式。
有没有类似的东西的作品?
如果没有,那么我怎样才能使自己的只读属性,将工作是这样的:
公共类AMODEL
{
[只读(真)]或只是[只读]
字符串aProperty {搞定;组;}
}
这样我就可以把这个:
@ Html.TextBoxFor(X => x.aProperty)
代替本(其中不工作):
@ Html.TextBoxFor(X => x.aProperty,新{@只读=只读})
或此(它的工作,但没有提交值):
@ Html.TextBoxFor(X => x.aProperty,新{禁用=禁用})
<一个href=\"http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html\">http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html
这样的可能?
<一href=\"http://stackoverflow.com/a/11702643/1339704\">http://stackoverflow.com/a/11702643/1339704
请注意:
公共字符串PropX {搞定;组; }
公共字符串PropY {搞定;组; }
}
我已经验证了这工作与后续的剃刀code:
@ Html.MyTextBoxFor(M = GT; m.PropX)
@ Html.MyTextBoxFor(M = GT; m.PropY)
这使得为:
&LT;输入ID =PropXNAME =PropX只读=只读类型=文本VALUE =Propx/&GT;
&LT;输入ID =PropYNAME =PropY类型=文本VALUE =PropY/&GT;
如果您需要禁用
而不是只读
您可以轻松地更改相应的帮手。
The ReadOnly attribute does not seem to be in MVC 4. The Editable(false) attribute does not work the way I would want it to.
Is there something similar that works?
If not then how can I make my own ReadOnly attribute that would work like this:
public class aModel
{
[ReadOnly(true)] or just [ReadOnly]
string aProperty {get; set;}
}
so I can put this:
@Html.TextBoxFor(x=> x.aProperty)
instead of this ( which does work ):
@Html.TextBoxFor(x=> x.aProperty , new { @readonly="readonly"})
or this ( which does work but values are not submitted ):
@Html.TextBoxFor(x=> x.aProperty , new { disabled="disabled"})
http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html
something like this maybe? http://stackoverflow.com/a/11702643/1339704
Note:
[Editable(false)] did not work
You can create a custom helper like this that will check the property for the presence of a ReadOnly
attribute:
public static MvcHtmlString MyTextBoxFor<TModel, TValue>(
this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
// in .NET 4.5 you can use the new GetCustomAttribute<T>() method to check
// for a single instance of the attribute, so this could be slightly
// simplified to:
// var attr = metaData.ContainerType.GetProperty(metaData.PropertyName)
// .GetCustomAttribute<ReadOnly>();
// if (attr != null)
bool isReadOnly = metaData.ContainerType.GetProperty(metaData.PropertyName)
.GetCustomAttributes(typeof(ReadOnly), false)
.Any();
if (isReadOnly)
return helper.TextBoxFor(expression, new { @readonly = "readonly" });
else
return helper.TextBoxFor(expression);
}
The attribute is simply:
public class ReadOnly : Attribute
{
}
For an example model:
public class TestModel
{
[ReadOnly]
public string PropX { get; set; }
public string PropY { get; set; }
}
I have verified this works with the follow razor code:
@Html.MyTextBoxFor(m => m.PropX)
@Html.MyTextBoxFor(m => m.PropY)
Which renders as:
<input id="PropX" name="PropX" readonly="readonly" type="text" value="Propx" />
<input id="PropY" name="PropY" type="text" value="PropY" />
If you need disabled
instead of readonly
you can easily change the helper accordingly.
这篇关于MVC 4剃刀数据注释只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!