1

I have some buttons in my view.

i want to disable some of the buttons based on some condition from controller. is there any way to do that?

1
  • Yep, there sure is, but you are more likely to get an answer to your problem if you post what you have tried and what exactly you are having trouble with. Commented Feb 23, 2011 at 9:23

3 Answers 3

5

Model:

public class MyModel {

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

}

Action:

public ViewResult Index() {

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

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

  return View(model);

}

View:

<button <%: Model.SomeProperty ? "disabled=\"disabled\"" : ""  %>>some button</button>
<button <%: Model.AnotherProperty ? "disabled=\"disabled\"" : ""  %>>Another button</button>
<button <%: ((bool)ViewData["Something"]) ? "disabled=\"disabled\"" : ""  %>>Something</button>
Sign up to request clarification or add additional context in comments.

2 Comments

The value of disabled attribute should be 'disabled' or empty, not 'true' / 'false'.
@David We now have the option of just putting "disabled" on elements as well with HTML5 if you wanted to include this.
2

This post is old, but this worked on mvc3 c# and can be useful:

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

Comments

1

Create same flag in controller and than pass it to the view. Inside view, read that flag, and disable button if needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.