我的應用程序是MVC3 ASPX C#;我試圖用啓用/禁用Html.EditorFor如下: 在Model:使用ViewData啓用/禁用Html.EditorFor
public bool IsReadOnly { get; private set; }
在控制器:
if (MySession.Current._progress == 100)
ViewData["ReadOnly"] = true;
在視圖:
... 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)%>
這並不行。我也試過:
<%: Html.EditorFor(model => model.Subject_motion, new { @disable = true })%>
也沒有工作!
最好的解決方案,因爲我也在同一視圖中有dropdownlist和複選框是使用@disable = true和@readonly = true或false,並且能夠使用ViewBag更改false的true。任何建議,感謝adavance。
你有沒有嘗試創建一個自定義EditorFor? http://stackoverflow.com/questions/5497183/how-to-create-custom-editor-display-templates-in-asp-net-mvc-3我通常只是靜態構建我的視圖,而不是構建自定義EditorFor,但這可能不是很好的面向對象的建議。 –