3

I am using this, an editor template (located in the Shared\EditorTemplates folder in my solution)

  <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
  <%=Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), ViewData )%>

and this in my view

  @Html.EditorFor(model => model.ModifiedDate)

how to make this field readonly in the view

4 Answers 4

5
<%= Html.EditorFor(x => x.ModifiedDate, new { @readonly = "readonly" }) %>

UPDATE:

OK, now that you sent me the sample project here are the issues:

  1. You have a spelling mistake in the ~/Views/Shared/EditorTempletes folder. It should be ~/Views/Shared/EditorTemplates.
  2. You editor template must be called DateTime.ascx and not a DateTime.aspx. And because of this the header must look like this (use <%@ Control ... instead of <%@ Page ...):

    <%@ Control 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" 
    %>
    <%= Html.TextBox(
        "", 
        (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), 
        ViewData
    ) %>
    
Sign up to request clarification or add additional context in comments.

3 Comments

@TJK, I have just tested this and it worked perfectly fine. Maybe you can send me a sample project illustrating the issue so that I can take a look at it?
how do i send a sample project?
@TJK, you can mail me at darin[DOT]dimitrov[AT]gmail[DOT]com.
3

You can use:

@Html.DisplayFor()

Comments

2

Decorate the property with the [HiddenInput] Attribute from the System.Web.Mvc namespace.

Comments

0

I use to show read-only information this way:

@Html.DisplayFor(model => model.CadastradoEm, new { @class = "form-control" })
@Html.HiddenFor(model => model.CadastradoEm)

You need to include a hidden input in addition to the display text, cause DisplayFor() doesn't generate a control that posts back ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.