MVC3 EditorOne to read

I want to do readOnly with EditorFor on the edit page.

I tried installing readonly and disconnecting as:

<div class="editor-field"> @Html.EditorFor(model => model.userName, new { disabled = "disabled", @readonly = "readonly" }) </div> 

However, this will not work. How can I disable editing this field?

Thank.

+52
asp.net-mvc-3 readonly
Apr 11 '12 at 15:33
source share
11 answers

There are no overloads with HTML attributes in the EditorFor html editor. In this case, you need to use something more specific, for example TextBoxFor:

 <div class="editor-field"> @Html.TextBoxFor(model => model.userName, new { disabled = "disabled", @readonly = "readonly" }) </div> 

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

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

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

 @model string @Html.TextBoxFor(m => m, new { disabled = "disabled", @readonly = "readonly" }) 
+68
Apr 11 '12 at 18:13
source share

This code is supported in MVC4 onwards.

 @Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control", disabled = "disabled", @readonly = "readonly" } }) 
+17
Nov 13 '15 at 17:12
source share

For those who are wondering why you want to use EditoFor, if you do not want it to be editable, I have an example.

I have it 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 this format, the only way this works is to use an editor, but I have a jquery datepicker for this β€œinput”, so it should be read-only so that users do not write incorrect 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 is in my finished function ...

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

Hope this will be helpful to someone out there. sorry for my English

+12
May 17 '13 at 16:14
source share

You can do it as follows:

 @Html.EditorFor(m => m.userName, new { htmlAttributes = new { disabled = true } }) 
+4
Aug 31 '16 at 7:02
source share

Here is how I do it:

Model:

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

View:

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

Expansion:

 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 does a bunch more things, for example adding a shortcut, adding the necessary class to this shortcut as necessary, adding DisplayFor , if the ReadOnly EditorFor property, if not, adding ValidateMessageFor and finally assuring it all of a Div that may have Html Attributes assigned to him ... my Views are super clean.

+3
Nov 04 '13 at 23:45
source share

I know the question says MVC 3, but it was 2012, so just in case:

Like MVC 5.1, you can now pass HTML attributes to EditorFor as follows:

 @Html.EditorFor(x => x.Name, new { htmlAttributes = new { @readonly = "", disabled = "" } }) 
+3
Apr 13 '16 at 23:35
source share

Create an EditorTemplate for a specific set of views (connected by one controller): enter image description here

In this example, I have a template for the Date, but you can change it to whatever you want.

Here is the code in Data.cshtml:

 @model Nullable<DateTime> @Html.TextBox("", @Model != null ? String.Format("{0:d}", ((System.DateTime)Model).ToShortDateString()) : "", new { @class = "datefield", type = "date", disabled = "disabled" @readonly = "readonly" }) 

and in the model:

 [DataType(DataType.Date)] public DateTime? BlahDate { get; set; } 
+1
Aug 05 '13 at 17:12
source share

Try using:

 @Html.DisplayFor(model => model.userName) <br/> @Html.HiddenFor(model => model.userName) 
+1
May 4 '16 at 10:22
source share

An old post that I know ... but now you can do this to keep alignment and everything looking consistent.

  @Html.EditorFor(model => model.myField, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }) 
+1
Oct 05 '16 at 16:11
source share

I think this is simpler than others using the [Editable (false)] attribute

eg:

  public class MyModel { [Editable(false)] public string userName { get; set; } } 
-one
Jan 17 '14 at 9:01
source share
 <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> 
-one
Aug 20 '14 at 19:32
source share



All Articles