Jimmy
Jimmy

Reputation: 9815

ASP.NET MVC DropDownLists and foreign keys

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?

Upvotes: 1

Views: 739

Answers (1)

AndreasN
AndreasN

Reputation: 2897

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.

Upvotes: 2

Related Questions