在 MVC 3 视图模型上使用 Editable 属性 [英] Using Editable attribute on MVC 3 view model

查看:14
本文介绍了在 MVC 3 视图模型上使用 Editable 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用属性将视图模型属性标记为只读,以便视图字段在渲染视图中是只读的.应用 System.ComponentModel.DataAnnotations.EditableAttribute 似乎是我需要的确切属性,但它似乎不起作用,即文本框字段仍然可编辑.我环顾四周,找不到答案,只有几个相关的问题.渲染视图时,下面应用的可编辑属性不起作用.

I'm looking to use attributes to mark view model properties as readonly so that the view fields are read only in the rendered view. Applying System.ComponentModel.DataAnnotations.EditableAttribute appears to be the exact attribute I need but it does not appear to work i.e. textbox fields are still editable. I've looked all around and can find no answers and only a few related questions. The editable attribute as applied below does not work when the view is rendered.

[Display(Name = "Last Name")]
[Editable(false, AllowInitialValue = true)]
public string LastName { get; set; }

我可以使用这样的视图辅助函数实现只读行为,但我更喜欢在模型属性上使用属性.

I can achieve the readonly behaviour using a view helper function like this but my preference is to use an attribute on the model property.

@functions {
    object getHtmlAttributes()
    {
    if (@ViewBag.Mode == "Edit")
    {
      return new {style = "width:100px;background:#ff6;", @readonly = "readonly"};
    }

    return new { style = "width:100px;" };  
}
} 

@Html.TextBoxFor(model => model.FirstName, getHtmlAttributes())

其他属性可以正常工作,包括自定义验证属性.你能告诉我数据注释可编辑属性是否在这种情况下工作,应该像上面应用的那样工作,还是需要做其他事情?谢谢.

Other attributes work perfectly ok including custom validation attributes. Can you tell me if the data annotations editable attribute works in this context, should just work as applied above or is there something else that needs to be done? Thanks.

推荐答案

EditableAttribute 文档 声明:

数据字段上存在 EditableAttribute 属性表明用户是否应该能够更改字段的值.

The presence of the EditableAttribute attribute on a data field indicates whether the user should be able to change the field's value.

此类既不强制也不保证字段是可编辑的.基础数据存储可能允许更改字段,而不管此属性是否存在.

This class neither enforces nor guarantees that a field is editable. The underlying data store might allow the field to be changed regardless of the presence of this attribute.

不幸的是,这意味着使用此属性对 MVC 中的验证没有任何影响.这感觉是错误的,但如果您考虑在 MVC 框架中实现它需要什么,它是有道理的.例如,在典型的编辑"视图中,用户执行初始 GET 请求,其中填充模型(通常来自数据库记录)并提供给视图以呈现给用户.然后用户进行一些编辑,然后提交表单.提交表单会导致从 POST 参数构造模型的 new 实例.验证器很难确保两个对象实例中的字段具有相同的值,因为其中一个实例(来自 GET 请求的第一个实例)已经被处理掉了.

Unfortunately, this means that the use of this attribute doesn't have any effect on validation in MVC. This feels wrong but it makes sense if you think about what it would take to implement in the MVC framework. For instance, in a typical "Edit" view, the user does an initial GET request where the Model is populated (usually from a DB record) and given to the View to be rendered to the user. Then the user makes some edits and then submits the form. Submitting the form causes a new instance of the Model to be constructed from the POST parameters. It would be very difficult for the validators to ensure the field has the same value in both object instances, because one of the instances (the first one from the GET request) has already been disposed of.

如果属性没有功能,为什么还要费心使用它呢?

我最好的猜测是,他们希望开发人员在他们的代码中使用它来表达意图.更实际的是,您还可以编写自己的自定义代码来检查该属性是否存在...

My best guess is that they expect developers to use it in their code to show intent. More practically, you can also write your own custom code to check for the presence of this Attribute...

AttributeCollection attributes = TypeDescriptor.GetAttributes(MyProperty);
if (attributes[typeof(EditableAttribute)].AllowEdit)
{
   // editable
}
else
{
   // read-only
}

还请记住,这些 DataAnnotation 属性不仅适用于 MVC 应用程序,它们还可用于许多不同类型的应用程序.尽管 MVC 没有对这个属性做任何特别的事情,其他框架已经为这个属性实现了功能/验证.

Also keep in mind that these DataAnnotation attributes are not only for MVC applications, they can be used for many different types of applications. Even though MVC doesn't do anything special with this Attribute, other frameworks have implemented functionality/validation for this attribute.

这篇关于在 MVC 3 视图模型上使用 Editable 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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