18

I am currently using the Html.EditorFor<> method for generating editor fields, however I would like to use something similar for displaying field information in a read-only format such as a details page, i.e. a label with the field name followed by a label with the field value.

Is there currently any equivalent in MVC for generating this? Or am I going to have to create a custom helper?

Thanks in advance.

Edit: I am aware of DisplayFor and LabelFor, is it just a case of manually having to combine these?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
StrictlySocial
  • 697
  • 2
  • 10
  • 19
  • 1
    I believe `DisplayFor` is exactly what you want. `LabelFor` will give you an element that just has the field name, while `DisplayFor` will output labels and values, but will not use form fields to do so. Just like `EditorFor`, you can create templates for `DisplayFor` if you do not like the default markup that comes out of `DisplayFor` – Brian Ball Feb 21 '11 at 13:33
  • 1
    You should select zihotki's answer - http://stackoverflow.com/a/5066283/467380. It's the best one, by far. – vbullinger Nov 20 '12 at 22:10

6 Answers6

36

Use

<%= Html.DisplayFor(model => model.Property) %>

Or if you want to see a readonly(disabled) textbox

<%= Html.TextBoxFor(model => model.Property, new { disabled="disabled", @readonly = "readonly" })%>
zihotki
  • 5,201
  • 22
  • 26
5

I may be missing something but can't you just use Html.LabelFor?

Edit: May need to be a combination using Html.LabelFor and Html.DisplayFor

Manatherin
  • 4,169
  • 5
  • 36
  • 52
  • Hi, unless I see anything else. I believe your suggestion of combining LabelFor and DisplayFor is the only option - this is what I was currently doing. – StrictlySocial Feb 21 '11 at 13:04
4

You can use

Html.DisplayFor(...)

Documentation:

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.displayextensions.displayfor.aspx

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
  • When you use only DislpayFor the page dont send the values to Controller. – Lucas Rodrigues Sena May 22 '13 at 14:48
  • The question doesn't imply he'd want to send the values back to the controller. What would be the point? They're read only so we already know what they are. – James Gaunt May 23 '13 at 16:04
  • 2
    There is a point. If your form fails to validate, when it is redrawn, that value will then be empty. Why not pass the value back to the controller instead of having to rebuild then entire view model or make any additional db calls to recalculate that value? I'm looking for a solution now. – Greg Sipes Sep 01 '15 at 14:49
3

MVC 5, Bootstrap 3 example.

<div class="col-md-6">
    @Html.LabelFor(m => m.UserModel.company, htmlAttributes: new {@class = "control-label tc-label"})
    @Html.EditorFor(m => m.UserModel.company, new {htmlAttributes = new { disabled = "disabled", @readonly = "readonly", @class = "form-control col-md-6"}})
    @Html.ValidationMessageFor(m => m.UserModel.company, "", new {@class = "text-danger"})
</div>
GGleGrand
  • 1,565
  • 1
  • 20
  • 45
  • 1
    Close, but I found that without explicitly stating that htmlAttributes is a `Dictionary` and leaving in the `class` string caused problems. Therefore amend this to `@Html.EditorFor(m => m.UserModel.company, new {htmlAttributes = new Dictionary() { {"disabled", "disabled"}, {"readonly", "readonly"}}})` And you should be golden. – Steve Pettifer Oct 09 '15 at 14:45
3

I have a textbox control which is bound to a property of the Model. However, I have a JQueryUI slider which I am using to populate the value, rather than have the user edit it directly. Therefore, I want this value to be read-only, but still bound to the model. I cannot apply html attributes using Html.EditorFor(...), I can use the Html.TextboxFor(...) method. However, if I set the HTML "disabled" attribute to a value of "disabled" (as per zihotki's solution), then the MVC framework by default does not bind the value in the textbox back to the bound property on the model when posting back to the controller method (see Model Binding With Disabled Textbox). My solution was to use Html.TextboxFor and set only the readonly attribute,

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

and then include a CSS entry for input:readonly to grey the read-only boxes out:

input:read-only
{
    background-color: #E6E6E6;
}
Community
  • 1
  • 1
Andy Thomas
  • 1,367
  • 2
  • 14
  • 30
0

Create an EditorFor Template that returns a string rather than a form field. Then use UIHint

E.g. This is StringReadOnly EditorFor template

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %><%:ViewData.TemplateInfo.FormattedModelValue.ToString()%>

In the model

[UIHint("StringReadOnly")]public string NoChangeMe {get;set}

Then you can just called the field with EditorFor and it will output a string.

Of course the field isn't submitted but that is what I wanted to happen.

GraemeMiller
  • 11,973
  • 8
  • 57
  • 111