viewModel中的下拉列表创建视图

时间:2015-07-01 08:25:13

标签: asp.net-mvc entity-framework razor drop-down-menu

我有一个“Leads”表,它与“Employees”表有一对多的关系。我还创建了一个leadViewModel类,因为我需要一些额外的Lead信息(如地址和电话等)。

以下是Lead类:

public partial class Leads
{
    [Key]
    public int LeadID { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    [NotMapped]
    public string FullName { get { return FirstName + " " + LastName; } }

    public int EmployeeID { get; set; }

    public virtual Employees Employees { get; set; }
}

员工类:

public partial class Employees
{
    public Employees()
    {
        this.Leads = new HashSet<Leads>();
    }

    [Key]
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [NotMapped]
    public string FullName { get { return FirstName + " " + LastName; } }

    public virtual ICollection<Leads> Leads { get; set; }
}

leadViewModel:

public class LeadViewModel
{
    public Leads _leads { get; set; }
    public Addresses _addresses { get; set; }
    public Phones _phones { get; set; }
    public Emails _emails { get; set; }

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

现在我想在“创建”视图中有一个下拉列表。我不知道该怎么做。这就是我所做的并且有错误。

LeadsController创建获取:

    // GET: Leads/Create
    public ActionResult Create()
    {
        ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FirstName");

        return View();
    }

LeadsController创建帖子:

    [HttpPost]
    [ValidateAntiForgeryToken]        
    public ActionResult Create([Bind(Include = "_leads,_addresses,_phones,_emails,EmployeesSelectListItem")] LeadViewModel leadVM)
    {            
        if (ModelState.IsValid)
        {
            using (TransactionScope leadScope = new TransactionScope())
            {
                db.Leads.Add(leadVM._leads);
                db.SaveChanges();

                leadScope.Complete();

                return RedirectToAction("Index");
            }
        }

        ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "FirstName", leadVM._leads.EmployeeID);

        return View(leadVM);
    }

最后是创建视图:

@model CL_AHR_CRM.ViewModels.LeadViewModel

...

   <div class="form-group">
        @Html.LabelFor(model => model._leads.EmployeeID, "EmployeeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(x => x._leads.EmployeeID, new SelectList(Model.EmployeesSelectListItem, "EmployeeID", "FirstName"), htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model._leads.EmployeeID, "", new { @class = "text-danger" })
        </div>
    </div>

我重复我的问题:在使用像这样的viewModel时,如何在视图中使用dropdownliast,因为显然我所做的不是方式而且不起作用。我收到错误:“对象引用未设置为对象的实例。”在创建视图中的Html.DropdownlistFor(...)。

1 个答案:

答案 0 :(得分:4)

您的视图基于LeadViewModel的类型,但在GET方法中,您没有将模型的实例传递给视图,更不用说在EmployeesSelectListItem填充DropDownListFor()属性了方法,new SelectList(Model.EmployeesSelectListItem, ...将抛出异常(Model的值为null

将视图模型属性更改为

public SelectList EmployeesSelectListItem { get; set; } // perhaps rename to EmployeeList?

然后将GET方法更改为

public ActionResult Create()
{
  LeadViewModel model = new LeadViewModel();
  model.EmployeesSelectListItem = new SelectList(db.Employees, "EmployeeID", "FirstName");
  return View(model);
}

同样在POST方法中你需要使用

leadVM.EmployeesSelectListItem = new SelectList(db.Employees, "EmployeeID", "FirstName");

当您返回视图时(即当ModelState无效时)

然后在视图中使用

@Html.DropDownListFor(x => x._leads.EmployeeID, Model.EmployeesSelectListItem, new { @class = "form-control" })