Amit Jog Amit Jog - 1 year ago 83
C# Question

Trouble with MVC 4 DropDownListFor

I am having trouble with displaying

in MVC 4 application.
It looks straightforward, However something is still missing which is not getting hold of.


public class vendorModel
public int vendorID { get; set; }
public int vendorTypeID { get; set; }
public string vendorName { get; set; }
public IEnumerable<vendorTypeModel> vendorTypes { get; set; }

public class vendorTypeModel
public int vendorTypeID { get; set; }
public string vendorTypeName { get; set; }


public ActionResult Vendor() {
vendorModel oVendor = new vendorModel();
oVendor.vendorTypes = blVendor.getAllVendorTypes();
return View(oVendor);

This is where I am fetching records from database and building and returning a
and Finally on View am using following code.

@Html.DropDownListFor(m => m.vendorTypeID, new SelectList(Model.vendorTypes,"vendorTypeID","vendorTypeName"))

What am I doing wrong? I am getting following results on screen.

enter image description here

Instead of names of vendors.

Please suggest whats wrong with my code.

Answer Source

I suspect that oVendor.vendorTypes = blVendor.getAllVendorTypes(); may return a wrong list back. Set a breakpoint and check the oVendor.vendorTypes please.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download