Ok, I'm very new to MVC so bear with me if I'm asking a ludicrously straightforward question. I'm trying to develop a project in MVC where certain controls on the view will keep state but are not actually part of the model. I want to display Asp.net Charts and xslt grids on a page based on the content of various dropdowns. The data for these would come from the model however the dropdownmenu's would be constant and so when you do a post on your given selections it saves the Dropdownlist selections and displays them as it would on a postback in Asp.net 2.0?
A:
If your using the standard Html helpers that it can get quite complicated as you will have to build your own SelectListItem class to pass into the view and tell the control which one is selected and pop it into a ViewData:
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem
{
Text = "Option1",
Value = "1",
Selected = (Request["dropdown"] == "Option1")
});
items.Add(new SelectListItem
{
Text = "Option2",
Value = "2",
Selected = (Request["dropdown"] == "Option2")
});
items.Add(new SelectListItem
{
Text = "Option3",
Value = "3",
Selected = (Request["dropdown"] == "Option3")
});
ViewData["items"];
You can then render this in your view as follows:
<%= Html.DropdownList("Dropdown1", (IEnumerable<SelectListItem>)ViewData["items"])
theouteredge
2009-11-06 10:39:35
Thanks, that's just what I was looking for :)
Israfel
2009-11-06 10:47:04
Thank you, my first accepted answer! :)Glad I could help!
theouteredge
2009-11-06 13:07:43
A:
One way I've been solving this in past was doing an ajax request on dropdownlist change event (serialize the form with dropdown selected values or just pass parameters in query string). Server (Controller actions) accept this request and return partial view with content. Client accepts that content and place (replace) into some div.
This way I never had to think about dropdownlist selected item, because I never reload whole page.
Misha N.
2009-11-06 10:43:22