Amit Jog Amit Jog - 2 months ago 10
C# Question

Trouble with MVC 4 DropDownListFor

I am having trouble with displaying

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

ViewModel

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; }
}


Controller

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


getAllVendorTypes
Method
This is where I am fetching records from database and building and returning a
List<vendorTypeModel>
object.
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

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