Strongly-typed binding to a DropDownListFor?
Asked Answered
O

4

9

Normally I would bind data to a DropDownListFor with a SelectList:

@Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, "OrderId", "ItemName"))

Is there any way to do this through strongly-typed lambdas and not with property strings. For example:

@Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, x => x.OrderId, x => x.ItemName))
Observer answered 6/12, 2013 at 9:6 Comment(1)
I don't think there is. The SelectList needs to know the Id property and description property names so it can bind those properties to the control/have it in its collection. you could perhaps instead make a custom displayeditor or extension methods instead but still think you need, at some level, to tell it what the ID property and display/description property names are on the object you are trying to bind. The other solution maybe to forget using the dropdownlistfor and just have a for loop spitting out the raw html instead...maybe.Copper
C
15

You could create the select list itself in the controller and assign it to a property in your view model:

public IEnumerable<SelectListItem> OrdersList { get; set; }

The code in your controller will look like this:

model.OrdersList = db.Orders
                     .Select(o => new SelectListItem { Value = o.OrderId, Text = o.ItemName })
                     .ToList();

In the view you can use it like this:

@Html.DropDownListFor(model => model.CustomerId, Model.OrderList)

I personally prefer this approach since it reduces logic in your views. It also keeps your logic 'stronly-typed', no magic strings anywhere.

Cruller answered 6/12, 2013 at 9:51 Comment(0)
H
0

There doesn't appear to be an overloaded constructor to allow for this. But there's no harm in manually specifying the DataTextField and DataValueField as a string is there?

Horsley answered 6/12, 2013 at 9:22 Comment(2)
There is a maintainability issue with using strings as a mismatch will throw a runtime exception. With strongly-typed binding this would be covered by the static compiler.Observer
Agree with the maintainability, the setting of the DataTextField and DataValueField has been around for quite sometime now from Winforms to Asp.net etc so perhaps finding a way to do this the way you're suggesting would be beneficial.Horsley
M
0

for that u can use a ViewBag for storing a data into your Drop Down Control

here is an example of it ..............

@Html.DropDownList("department", (IEnumerable<SelectListItem>)@ViewBag.DepartmentList, new { value = @ViewBag.department, style = "width:140px", onchange = "OnDepartmentChange(this)" })

in controller in Index method you can write like

 ViewBag.Department = department;
Mozell answered 6/12, 2013 at 9:23 Comment(1)
ya defiantly dude can we share our SkypeMozell
W
0

When we use strongly typed views, we can use the @Html.DropDownListFor() method. This helper method will need the list of departments to first populate the dropdown and then set the employee’s department id passed in model object as selected item.

    public ActionResult edit(int id)
{
    Employee emp = db.Employees.Where(e => e.EmployeeId == id).FirstOrDefault();
    ViewBag.DepartmentListItems = db.Departments.Distinct().Select(i => new SelectListItem() { Text = i.DepartmentName, Value = i.DepartmentId.ToString() }).ToList();
    return View(emp);
}

The list items in view bag will be used to bind the dropddownlist and html helper will set the DepartmentId based on the Employee model passed to the view. View Code below.

@Html.DropDownListFor(model => model.DepartmentId, ViewBag.DepartmentListItems as IEnumerable<SelectListItem>,"Select")
Westfall answered 15/10, 2018 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.