Using Editable attribute on MVC 3 view model

后端 未结 5 1684
故里飘歌
故里飘歌 2020-12-10 05:58

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.DataAnnot

相关标签:
5条回答
  • 2020-12-10 06:20

    Just semi-solved this issue myself.

    [HiddenInput(DisplayValue=true)]

    The field is displayed but not editable.

    0 讨论(0)
  • 2020-12-10 06:27

    The EditableAttribute documentation states:

    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.

    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.

    Well if the Attribute has no functionality, why even bother to use it?

    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
    }
    

    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.

    0 讨论(0)
  • 2020-12-10 06:33

    Found that the use of Editable instead Readonly on the model works exactly the same.

    [ReadOnly(true)] //or
    [Editable(false)]
    public string Name { get; set; }
    

    This syntax does work when interrogating a property attribute on the view itself. Also works when attribute is Editable(true)

    @if (ViewData.ModelMetadata.Properties.Where(p => p.PropertyName == "Name").First().IsReadOnly)
    { 
        @Html.TextBoxFor(model => model.Name, new { style = "width:190px; background-color: #ffffd6", @readonly =  "readonly" })
    } 
    else
    {
        @Html.TextBoxFor(model => model.Name, new { style = "width:190px; " })
    }
    

    Using an editor template here a simple string template:

    @model String
    @{
    IDictionary<string, object> htmlAttributes = new Dictionary<string, object>();
    if (ViewData.ModelMetadata.IsReadOnly) //this will be looking at the actual property not the complete model
    {
    htmlAttributes.Add("style", "width:100px; background-color:#ffffd6");
    htmlAttributes.Add("readonly", "readonly");
    @Html.TextBox("", Model, htmlAttributes)
    }
    
    0 讨论(0)
  • 2020-12-10 06:34

    Do you have a different create scenario? Any particular reason you are allowing an initial value? I ask because the documentation says:

    Because you typically want both properties to contain the same value, the AllowInitialValue property is set to the value of AllowEdit in the class constructor.

    I'm thinking if you set it to false and don't explicitly declare the AllowInitialValue it will work.

    0 讨论(0)
  • 2020-12-10 06:42

    don't know if you've figured this out yet, but we use

    System.ComponentModel.ReadOnlyAttribute
    

    usage

    [ReadOnly(true)]
    
    0 讨论(0)
提交回复
热议问题