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

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

问题描述

我期待使用属性为只读标记视图模型属性,以便视场只在渲染视图读取。施加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; }

我可以使用这样的视图助手功能实现只读的行为,但我的preference是在模型属性使用属性。

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.

推荐答案

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.editableattribute.aspx\"相对=nofollow> EditableAttribute文档规定:

上的数据字段EditableAttribute属性的presence指示用户是否应该能够改变字段的值

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

这个类没有强制执行,也不保证一个字段是可编辑的。底层数据存储可能会允许该字段为改变无论此属性的presence的。

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参数构建的模型的实例。这将是非常困难的验证,以确保字段具有在两个对象实例相同的值,因为其中一个实例(从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.

那么,如果该属性没有任何功能,为什么还要费心去使用它?

我最好的猜测是,他们希望开发人员能够使用在他们的code以示意图。更实际的,你也可以编写自己的定制code检查此属性的presence ...

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没有做什么特别的这个属性,其他框架已经实现的功能/验证此属性

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

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