C# MVC 4 剃刀数据注释只读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18515441/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:27:50  来源:igfitidea点击:

MVC 4 razor Data Annotations ReadOnly

c#jquery-mobileasp.net-mvc-4razordata-annotations

提问by Soenhay

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.

ReadOnly 属性似乎不在 MVC 4 中。 Editable(false) 属性不能按照我希望的方式工作。

Is there something similar that works?

有没有类似的东西有效?

If not then how can I make my own ReadOnly attribute that would work like this:

如果不是,那么我如何制作自己的 ReadOnly 属性,它会像这样工作:

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

http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html

something like this maybe? https://stackoverflow.com/a/11702643/1339704

也许像这样的事情? https://stackoverflow.com/a/11702643/1339704

Note:

笔记:

[Editable(false)] did not work

[Editable(false)] 不起作用

采纳答案by asymptoticFault

You can create a custom helper like this that will check the property for the presence of a ReadOnlyattribute:

您可以创建一个像这样的自定义帮助程序,它将检查属性是否存在ReadOnly属性:

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 disabledinstead of readonlyyou can easily change the helper accordingly.

如果您需要disabledreadonly您可以轻松地相应地更改助手。

回答by KerSplosh

You could create your own Html Helper Method

您可以创建自己的 Html Helper Method

See here: Creating Customer Html Helpers

请参阅此处: 创建客户 Html 帮助程序

Actually - check out this answer

实际上 - 看看这个答案

 public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(
         this HtmlHelper<TModel> helper, 
         Expression<Func<TModel, TProperty>> expression)
    {
        return helper.TextBoxFor(expression, new {  @readonly="readonly" }) 
    }