2012-11-16 31 views
0

我的應用程序是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。

+0

你有沒有嘗試創建一個自定義EditorFor? http://stackoverflow.com/questions/5497183/how-to-create-custom-editor-display-templates-in-asp-net-mvc-3我通常只是靜態構建我的視圖,而不是構建自定義EditorFor,但這可能不是很好的面向對象的建議。 –

回答

1

你可以試試這個:

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

這裏的EditorFor和TextBoxFor之間的區別的解釋:

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

+0

TextBoxFor不適用於我的應用程序,因爲我使用顯示爲複選框的位域;而不是改變所有的領域;我做了一個新的視圖,並將EditorFor替換爲Html.DisplayFor,並將其從控制器重定向到該視圖。謝謝 – hncl