我们可以禁用控制器的按钮吗? - mvc

问题描述:

我在我看来有一些按钮。我们可以禁用控制器的按钮吗? - mvc

我想禁用一些基于控制器的一些条件的按钮。 有没有办法做到这一点?

+0

是的,可以肯定的,但是如果你发布你已经尝试过的以及你有什么问题,你更有可能得到你的问题的答案。 – 2011-02-23 09:23:16

型号:

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

在控制器中创建相同的标志并将其传递给视图。在内部视图中,读取该标志,并在需要时禁用按钮。

这个职位是旧的,但这个工作在MVC3 C#和可能是有用的:

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