2

I have the dropdowns in my Create() View working perfect.

But in the Edit() View I can't get the Data that was submited during the Create() to show up in DropDowns with the Value enterened upon Create()

I just have textboxs in place at the moment And would really like to have Data Represented in a dropdown for easy selection.

Here is one example:

Create() View - One dropdown is for EmployeeTypes, and stores selected to EmployeeTypeId

Now How do I get that to show up in the Edit() View as the same dropdown, but with Value of EmployeeId already selected?


I have a EmployeeViewModel for the Create() View

But I am just passing the model directly into the Edit() View

Should I create some kind of Employee "partial class" for the Edit() View? to handle the IEnumerable Lists?

and set:

var employeeTypes = context.EmployeeTypes.Select(et => new SelectListItem
            {
                Value = et.EmployeeTypeId.ToString(),
                Text = et.Type.ToString()
            });

Or should I pass them in as ViewData?

If so how to do you pass a List in as ViewData and get it to display as an @Html.DropDownList with the Value passed in from the @Model as the defualt value?

Timothy Green
  • 1,851
  • 5
  • 17
  • 22

2 Answers2

8

I ended up implimenting this way, and it worked like a dream.

Controller Code:

 SelectList typelist = new SelectList(context.CompanyType.ToList(), "CompanyTypeId", "Type", context.CompanyType);
            ViewData["CompanyTypes"] = typelist;

View Code:

@Html.DropDownList("CompanyTypeId", (IEnumerable<SelectListItem>) ViewData["CompanyTypes"])
Timothy Green
  • 1,851
  • 5
  • 17
  • 22
3

There may be bugs in this code - I haven't tested it - but what you basically want to do is:

var etId = ???  // EmployeeTypeId from your model
var employeeTypes = context.EmployeeTypes.Select(et => new SelectListItem
        {
            Value = et.EmployeeTypeId.ToString(),
            Text = et.Type.ToString(),
            Selected = et.EmployeeTypeId == etId
        });
ViewData["EmployeeTypeList"] = employeeTypes.ToList();

Then in your view you can do

@Html.DropDownList("EmployeeType", ViewData["EmployeeTypeList"])
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Am I missing some kind of Refernce? I am using the @Html.DropDownBox() in my Create() View just fine. Compiler Error Message: CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments – Timothy Green Mar 22 '11 at 05:58
  • This is the Sytax from my View():
    @Html.DropDownList("EmployeeTypeId", ViewData["EmployeeTypeList"]) @Html.ValidationMessageFor(model => model.EmployeeTypeId)
    – Timothy Green Mar 22 '11 at 06:00
  • @Timothy Green - That's funny. The DropDownList override is there in MVC2. I'll do some digging around in MVC3 and see what I can find. – Andrew Cooper Mar 22 '11 at 06:25
  • @Timothy Green - Do you have a reference to System.Web.Mvc.Html in your project (Web.config)? – Andrew Cooper Mar 22 '11 at 23:19