hncl
hncl

Reputation: 2295

Enable/Disable Html.EditorFor using ViewData

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.

Upvotes: 0

Views: 9626

Answers (1)

dotnetesse
dotnetesse

Reputation: 266

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

Upvotes: 1

Related Questions