开发者

Getting a dropdown list to work with codefirst 4.1 MVC3

Im trying to get my head around the entity framework 4.1 codefirst approach for MVC3.

I am trying to add a dropdownlist into a create view with little luck.

I have a model looking like

public class BusinessModel
{
    public int Id { get; set; }

    [Required]
    public string BusinessName { get; set; }
    [Required]
    public string PhoneNumber { get; set; }


    public int BusinessTypeId { get; set; }
    public virtual BusinessTypeModel BuinessTypeModel { get; set; }

}

public class BusinessTypeModel
{
    public int Id { get; set; }
    public string Busi开发者_开发知识库nessType { get; set; }
}

My dropdown list looks like

     @Html.DropDownListFor(model => model.BusinessTypeId,
         ((IEnumerable<CRM.Models.BusinessTypeModel>)ViewBag.BuinessTypes)
    .Select(option => new SelectListItem
    {

        Text = (option == null ? "None" : option.BusinessType),

        Value = option.Id.ToString(),

        Selected = (Model != null) && (option.Id == Model.BusinessTypeId)

    }), "Select Business Type...")

I havent done anything yet with my controller so its vanilla

 public ActionResult Create()
    {
        return View();
    } 

However I get a null error

Value cannot be null. Parameter name: source

Now I assume this is null because I am not binding the dropdown list correctly however I am not sure where I am going wrong.

Could someone please help with what im doing wrong?


you use "ViewBag.BuinessTypes" in your View,

Maybe you should assign a valeu of ViewBag.BuinessTypes in Create Controller?

like:

 public ActionResult Create()
    {
        ViewBag.BuinessTypes = ...;        
        return View();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜