maintaning drop down selected state after post back in MVC razor?

19,821

One way to do it is, in your controller, return the submitted value in the model. This means your dropdownlist should be hooked up to your viewmodel.

ViewModel:

public class MyViewModel
{
    // more properties...
    public string Make {get;set;}
    // more properties
}

Controller:

[HttpPost]
public ActionResult MyAction(MyViewModel model)
{
    // do postback stuff
    return View(model); // model.Make is set to whatever was submitted and will be returned   
}

Html:

@model Models.MyViewModel

@(Html.DropDownListFor(m => m.Make, 
     new SelectList(Model.Makes, "CCultureId", "CTitle", Model.Make), 
     "All", new {@class="span3"}))
Share:
19,821

Related videos on Youtube

Toubi
Author by

Toubi

Updated on September 16, 2022

Comments

  • Toubi
    Toubi over 1 year

    In a MVC 4 Web I have drop-down lists with the below sample code:

    @(Html.DropDownList("Condition2", new SelectList(Model.Makes, "CCultureId", "CTitle"), "All",new {@class="span3"}))
    

    I have All as a first option in select and on button press, page shows data in it. After post back, drop downs got reset on button press, can you please guide me how to make drop down keeping its state even after page post backs (I understand in MVC4 there are no postback, I m reffering it as a round trip to server).