tags:

views:

108

answers:

1

I am using a drop down list in my MVC app to select from a set of areas for editing or creating an entry

the code looks like this:

<%= Html.DropDownList("LocationID", ViewData["Areas"] as SelectList) %>

ViewData["Areas"] = new SelectList(AreaHelper.Areas, tablet.LocationID);

I am having issues with saving and updating the current locationID to the new selected value of the DDL, also when choosing the selected item on load when editing a current entry

any pointers?

+1  A: 

This is how i do it.

public class ViewModel
{
  public long Location { get;set;}
}

public ActionResult()
{
  ViewData["Location"] = new List<SelectListItem>
  {
     new SelectListItem{ Name = "US", Value = "1" },
  }
  return View(new ViewModel() { Location = GetOldValue() })
}

--

<%= Html.DropDOwnList("Location") %>

This workes when using model binding and typed views.

AndreasN
could you explain a bit more in depth? is there a way to create the select list and loop through results from a linq to sql call to add to that select list?
Jimmy
in otherwords i would rather not manually enter all of the areas in the selectlist construction, i would like a more dynamic approach
Jimmy
I followed your method and its working nicely so far--the only problem im having is when saving its not changing the ID field in the database for that item, to reflect the different area selected in the drop down. Here is my code:public class TabletViewModel { //properties public Tablet Tablet { get; private set; } public SelectList Areas { get; private set; } //init public TabletViewModel(Tablet tablet) { Tablet = tablet; Areas = new SelectList(AreaHelper.Areas, tablet.LocationID); }
Jimmy