如何在MVC 3中将数据库中的值绑定到下拉列表?

3

我正在尝试在mvc3中从数据库绑定下拉列表。

我有两个表。 tblEmp: EmpID(pk), EName, Age, Address, EmailID, DeptID(fk)。

tblDept DeptID(pk), DeptName, DeptHead。

我正在尝试创建一个基本的雇员应用程序,包括雇员的姓名、年龄、地址、电子邮件和部门名称。我正在尝试从另一个表中绑定部门名称下拉列表。

这是我的模型:

namespace MvcEmployeeApplication.Models
{

   public class UandPcompare
{
    public int EmpID { get; set; }
    public string EName { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public string EmailID { get; set; }
    public int DeptID { get; set; }
    public string DeptName { get; set; }
    public string DeptHead { get; set; }

    public IList<SelectListItem> Drp_DeptNames { get; set; }
}
}

这是控制器:

    [HttpGet]
    public ActionResult Create()
    {
        FillDeptName();        
        return View();
    }

    [HttpPost]
    public ActionResult Create(tblEmployee tblEmp)
    {
        test.Entry(tblEmp).State = System.Data.EntityState.Added;
        test.SaveChanges();
        return RedirectToAction("Index");

    }

    public ActionResult FillDeptName()
    {
        UandPcompare filldeptNme = new UandPcompare();            
        filldeptNme.Drp_DeptNames = (from DptName in test.tblDepts
                                     select new SelectListItem()
                                     {
                                         Text = DptName.DeptName,
                                         Value = SqlFunctions.StringConvert((double)DptName.DeptID)
                                     }).ToList<SelectListItem>();
        return View("Create");
    }

这是MyView:

@model MvcEmployeeApplication.Models.UandPcompare
@{
ViewBag.title = "Edit";
}
<h2> Create </h2>
@using (Html.BeginForm())
{
<fieldset>
<legend> Create </legend>

<div>
  Employee ID:  @Html.DisplayFor(model => model.EmpID)
</div>
<div>
  Employee Name:  @Html.EditorFor(model => model.EName)
</div>
<div>
  Email-ID:  @Html.EditorFor(model => model.EmailID)
</div>
<div>
  Address: @Html.EditorFor(model => model.Address)
</div>
<div>
  Dept Name: @Html.DropDownList("DeptName", Model.Drp_DeptNames, "Select")
</div>
<p>
<input type="submit" value="Create" />
</p>

<div>
@Html.ActionLink("Back to Index", "Index");
</div>

我认为最好的方法是编写一个JavaScript,它将调用一个“Action”方法,该方法将返回一个对象的Json数组。然后,您可以使用JQUERY将它们绑定到下拉列表中。 - Nilesh
不认为这是一个好主意,既然他可以仅从操作中传递SelectList,那么还需要ajax吗? - Nitin Varpe
1个回答

2

无法得知您遇到了什么错误。

您没有将任何模型传递给您的视图。

public ActionResult FillDeptName()
    {
        UandPcompare filldeptNme = new UandPcompare();            
        filldeptNme.Drp_DeptNames = (from DptName in test.tblDepts
                                     select new SelectListItem()
                                     {
                                         Text = DptName.DeptName,
                                         Value = SqlFunctions.StringConvert((double)DptName.DeptID)
                                     }).ToList<SelectListItem>();
        return View("Create",filldeptNme);//pass model to view here
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接