MVC3 EditorFor readOnly

前端 未结 12 1653
谎友^
谎友^ 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:58

    This code is supported in MVC4 onwards

    @Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control", disabled = "disabled", @readonly = "readonly" } })
    
    0 讨论(0)
  • 2020-12-05 09:59

    Here's how I do it:

    Model:

    [ReadOnly(true)]
    public string Email { get { return DbUser.Email; } }
    

    View:

    @Html.TheEditorFor(x => x.Email)
    

    Extension:

    namespace System.Web.Mvc
    {
        public static class CustomExtensions
        {
            public static MvcHtmlString TheEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
            {
                return iEREditorForInternal(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            }
    
            private static MvcHtmlString iEREditorForInternal<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
            {
                if (htmlAttributes == null) htmlAttributes = new Dictionary<string, object>();
    
                TagBuilder builder = new TagBuilder("div");
                builder.MergeAttributes(htmlAttributes);
    
                var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    
    
                string labelHtml = labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression).ToHtmlString();
    
                if (metadata.IsRequired)
                    labelHtml = Html.LabelExtensions.LabelFor(htmlHelper, expression, new { @class = "required" }).ToHtmlString();
    
    
                string editorHtml = Html.EditorExtensions.EditorFor(htmlHelper, expression).ToHtmlString();
    
                if (metadata.IsReadOnly)
                    editorHtml = Html.DisplayExtensions.DisplayFor(htmlHelper, expression).ToHtmlString();
    
    
                string validationHtml = Html.ValidationExtensions.ValidationMessageFor(htmlHelper, expression).ToHtmlString();
    
                builder.InnerHtml = labelHtml + editorHtml + validationHtml;
    
                return new MvcHtmlString(builder.ToString(TagRenderMode.Normal));
            }
        }
    }
    

    Of course my editor is doing a bunch more stuff, like adding a label, adding a required class to that label as necessary, adding a DisplayFor if the property is ReadOnly EditorFor if its not, adding a ValidateMessageFor and finally wrapping all of that in a Div that can have Html Attributes assigned to it... my Views are super clean.

    0 讨论(0)
  • 2020-12-05 09:59
    <div class="editor-field">
            @Html.EditorFor(model => model.userName)
    </div>
    

    Use jquery to disable

    <script type="text/javascript">
       $(document).ready(function () {
          $('#userName').attr('disabled', true);
         });
    </script>
    
    0 讨论(0)
  • 2020-12-05 10:04

    For those who wonder why you want to use an EditoFor if you don`t want it to be editable, I have an example.

    I have this in my Model.

        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0: dd/MM/yyyy}")]
        public DateTime issueDate { get; set; }
    

    and when you want to display that format, the only way it works is with an EditorFor, but I have a jquery datepicker for that "input" so it has to be readonly to avoid the users of writting down wrong dates.

    To make it work the way I want I put this in the View...

         @Html.EditorFor(m => m.issueDate, new{ @class="inp", @style="width:200px", @MaxLength = "200"})
    

    and this in my ready function...

         $('#issueDate').prop('readOnly', true);
    

    I hope this would be helpful for someone out there. Sorry for my English

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

    Try using:

    @Html.DisplayFor(model => model.userName) <br/>
    @Html.HiddenFor(model => model.userName)
    
    0 讨论(0)
  • 2020-12-05 10:07

    You can do it this way:

    @Html.EditorFor(m => m.userName, new { htmlAttributes = new { disabled = true } })
    
    0 讨论(0)
提交回复
热议问题