2011-02-23 45 views
0

我在我看來有一些按鈕。我們可以禁用控制器的按鈕嗎? - mvc

我想禁用一些基於控制器的一些條件的按鈕。 有沒有辦法做到這一點?

+0

是的,可以肯定的,但是如果你發佈你已經嘗試過的以及你有什麼問題,你更有可能得到你的問題的答案。 – 2011-02-23 09:23:16

回答

4

型號:

public class MyModel { 

    public bool SomeProperty { get; set; } 
    public bool AnotherProperty { get; set; } 

} 

操作:

public ViewResult Index() { 

    //strongly typed example 
    var model = new MyModel { 
    SomeProperty = true, 
    AnotherProperty = false 
    } 

    ViewData["Something"] = true; //view data example 

    return View(model); 

} 

查看:

<button <%: Model.SomeProperty ? "disabled=\"disabled\"" : "" %>>some button</button> 
<button <%: Model.AnotherProperty ? "disabled=\"disabled\"" : "" %>>Another button</button> 
<button <%: ((bool)ViewData["Something"]) ? "disabled=\"disabled\"" : "" %>>Something</button> 
+1

禁用屬性的值應該是「禁用」或空的,而不是「真」/「假」。 – 2011-02-23 09:28:35

+0

@Jakub我糾正了答案。 – 2011-02-23 10:15:12

+0

@David我們現在可以選擇僅在HTML5中加入「禁用」元素,如果您想包含此元素。 – AnotherDeveloper 2015-05-15 16:56:18

1

在控制器中創建相同的標誌並將其傳遞給視圖。在內部視圖中,讀取該標誌,並在需要時禁用按鈕。

2

這個職位是舊的,但這個工作在MVC3 C#和可能是有用的:

<button @Html.Raw(Model.SomeProperty ? "disabled=\"disabled\"" : "") >a button</button> 
相關問題