MVC3 EditorFor readOnly

前端 未结 12 1684
谎友^
谎友^ 2020-12-05 09:04

I want to make readOnly with EditorFor in edit page.

I tried to put readonly and disabled as:

@Html.Editor
12条回答
  •  臣服心动
    2020-12-05 09:41

    The EditorFor html helper does not have overloads that take HTML attributes. In this case, you need to use something more specific like TextBoxFor:

    @Html.TextBoxFor(model => model.userName, new { disabled = "disabled", @readonly = "readonly" })

    You can still use EditorFor, but you will need to have a TextBoxFor in a custom EditorTemplate:

    public class MyModel
    {
        [UIHint("userName")]
        public string userName { ;get; set; }
    }
    

    Then, in your Views/Shared/EditorTemplates folder, create a file userName.cshtml. In that file, put this:

    @model string
    @Html.TextBoxFor(m => m, new { disabled = "disabled", @readonly = "readonly" })
    

提交回复
热议问题