0

my application is MVC3 ASPX C#; I am trying to enable/disable Html.EditorFor using the following: In the Model:

 public bool IsReadOnly { get; private set; }

In the controller:

   if (MySession.Current._progress == 100)
       ViewData["ReadOnly"] = true;

In the view:

... Inherits="System.Web.Mvc.ViewPage<Models.CTAHFormEdit>" %>
 <script runat="server" type="text/C#">
        bool isReadOnly = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool?)this.ViewData["ReadOnly"] == true)this.isReadOnly = true;
        }
    </script>
    <%: Html.EditorFor(model => model.Vessel_calcification, Model.IsReadOnly)%>

This did not work. I also tried:

  <%: Html.EditorFor(model => model.Subject_motion, new { @disable = true })%>

did not work also!

The best solution, becasue I also have dropdownlist and checkbox in the same view is to use @disable = true and @readonly = true or false and be able to change true of false using a ViewBag. Any suggestions, thanks in adavance.

hncl
  • 2,295
  • 7
  • 63
  • 129
  • Have you attempted to create a custom EditorFor? http://stackoverflow.com/questions/5497183/how-to-create-custom-editor-display-templates-in-asp-net-mvc-3 I generally just build my view statically instead of building the custom EditorFor, but that's probably not good OO advice. – Kaizen Programmer Nov 16 '12 at 20:22

1 Answers1

1

You can try this:

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

Here's an explanation of the difference between EditorFor and TextBoxFor:

Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor

Community
  • 1
  • 1
dotnetesse
  • 256
  • 6
  • 16
  • TextBoxFor did not work for my application becasue I am using bit fields that shows as a check box; rather than changing all fields; I made a new view and replaced EditorFor to Html.DisplayFor, and just redirected to that veiew from the controller. Thanks – hncl Nov 17 '12 at 21:27