【发布时间】:2014-09-16 15:55:18
【问题描述】:
您好,我是 MVC 4 的新手,在尝试使用 Html.DropDownList 时遇到了错误。
这是我当前的代码。
工作模型:
namespace Payroll.CORE
{
public class Job
{
public int JobID { get; set; }
public string JobTitle { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
}
员工模型:
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int PhoneNumber { get; set; }
public int CellNumber { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int PostalCode { get; set; }
public string BankName { get; set; }
public int BankAccountNumber { get; set; }
public string PayInterval { get; set; }
public double NormalRate { get; set; }
public double OvertimeRate { get; set; }
public double SetHours { get; set; }
public System.DateTime DateOfEmployment { get; set; }
public System.DateTime LastPaymentRecieved { get; set; }
public string Active { get; set; }
public int JobID { get; set; }
public virtual ICollection<Absent> Absents { get; set; }
public virtual ICollection<Bonus> Bonuses { get; set; }
public virtual Job Job { get; set; }
public virtual ICollection<Holiday> Holidays { get; set; }
public virtual ICollection<HoursWorked> HoursWorkeds { get; set; }
public virtual ICollection<Payrolls> Payrolls { get; set; }
public virtual ICollection<Deduction> Deductions { get; set; }
查看:
<div class="editor-label">
@Html.LabelFor(model => model.JobID)
</div>
<div class="editor-field">
@Html.DropDownList("ListSL")
</div>
控制器:
[HttpGet]
public ActionResult Create()
{
List<SelectListItem> jobs = new List<SelectListItem>();
List<Job> list = bll.GetJobCatergories();
foreach (var a in list)
{
jobs.Add(new SelectListItem { Text = a.JobTitle, Value = a.JobID.ToString() });
}
ViewBag.ListSL = jobs;
return View();
}
我现在无法将 JobID 的值添加到我的模型中。我曾尝试使用 @Html.DropDownListFor 但无法使用 SelectListItem 填充它。
我真的没有问题让它与隐藏字段或任何东西一起工作,所以任何帮助都将不胜感激。
提前致谢。
编辑: 抱歉应该更清楚一点。我正在创建一名员工,并且该员工被分配了一份工作。因此,我尝试将选定的 Items 列表值添加到 Employee 中的 Value int JobID 中。
值也显示在下拉列表中,我需要在提交时将所选值添加到员工模型中。
【问题讨论】:
-
您在此处发布了控制器,而不是模型。让我们看一下模型,我们会更好地了解需要什么。
-
@barrick 是的,抱歉,这正是我想添加的计划,但似乎我的注意力还不够集中。
标签: c# asp.net asp.net-mvc asp.net-mvc-4 drop-down-menu