简体   繁体   中英

Html.EditorFor<> equivalent for viewing read-only data?

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, ie 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?

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" })%>

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

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>

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;
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM