It really depends on how your controls are displayed. In practice, we do something similar, except that we install read-only controls. This will allow us to reuse show (read-only) and edit views.
The way I personally would recommend doing this is to have a read-only flag that is set in the view using the value in ViewData.
From there write some helper methods to distinguish between disabled and uninfected markup. You can create this markup yourself or wrap existing HtmlHelper methods provided by ASP.NET MVC.
// In your controller ViewData["DisableControls"] = true; <%-- In your view --%> <% bool disabled = ViewData["DisableControls"] as bool; %> ... <%= Html.TextBox("fieldname", value, disabled) %> <%= Html.CheckBox("anotherone", value, disabled) %> // In a helper class public static string TextBox(this HtmlHelper Html, string fieldname, object value, bool disabled) { var attributes = new Dictionary<string, string>(); if (disabled) attributes.Add("disabled", "disabled"); return Html.TextBox(fieldname, value, attributes); }
As we do this, use the Page_Load () function, as in WebForms, to disable server controls. We have created some custom server controls for processing form fields. This was in early childhood ASP.NET MVC, and I would not recommend doing this, but it is an alternative.
Samantha branham
source share