Null Pointer
Null Pointer

Reputation: 9309

can we disable a button from controller? - mvc

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?

Upvotes: 1

Views: 12288

Answers (3)

PnP
PnP

Reputation: 625

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

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

Upvotes: 2

David Glenn
David Glenn

Reputation: 24532

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>

Upvotes: 5

šljaker
šljaker

Reputation: 7374

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

Upvotes: 1

Related Questions