Manuel Reis
Manuel Reis

Reputation: 574

Empty Select List - Disabled by default

I am using asp.net MVC, and I am trying to create an empty SelectList that is disabled by default.

The idea is to have a cascading dropdown list, and when I call my Create controller on GET, my second dropdown list is not available. However, if I am calling Create on POST, I want a jQuery function to populate that dropdown.

In my controller, I have the following line to initialize the Dropdown:

ViewBag.SOME_VARIABLE = Enumerable.Empty();

I was trying to avoid disabling it in my view, doing something like:

Html.DropDownList("SOME_VARIABLE", null, new { @disabled = "disabled" })

because I do not want it to be disabled on POST calls.

In the end, I want my HTML to be rendered like:

<select ... disabled></select>

If my requirements are not possible, I am open to suggestions.

Upvotes: 0

Views: 614

Answers (1)

ahmed abdelnabi
ahmed abdelnabi

Reputation: 51

Try to pass ViewBag.disabled = false in the POST Action and in your view just print the viewbag value in hidden field and read it using jquery after page load and then you can enable the drop down or leave it disabled

Upvotes: 1

Related Questions