我需要很大的帮助,我是 Asp.net MVC 4 应用程序开发的新手,实际上我在单击提交按钮后将下拉列表选择的值保存在数据库中时遇到了问题。
我使用调试指针检查 HTTP post 对象中的值,但它不包含下拉列表选择值它总是在除法原始中显示空值我需要一些专家建议来解决这个问题我通过几个示例并尝试了几次但是我仍然没有适当的解决方案。
型号类:
public partial class tblEmployee
{
public int EmployeeId { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Nullable<System.DateTime> DateOfBirth { get; set; }
public Nullable<System.DateTime> DateOfJoin { get; set; }
public string Position { get; set; }
public string Office { get; set; }
public string Division { get; set; }
public Nullable<decimal> Salary { get; set; }
public virtual tblDivision Divisions { get; set; }
}
public partial class tblDivision
{
public int value { get; set; }
public string Division { get; set; }
public Nullable<int> SelectId { get; set; }
}
控制器类:
namespace EmpiteHrSystem.Controllers
{
public class EmployeeController : Controller
{
private EmpiteContext db = new EmpiteContext();
public ActionResult Create()
{
ViewBag.DivisionOptions = new SelectList(db.tblDivisions, "value","Division");
return View();
}
//
// POST: /Employee/Create
[HttpPost]
public ActionResult Create(tblEmployee tblemployee)
{
if (ModelState.IsValid)
{
try
{
db.Entry(tblemployee).State = EntityState.Added;
db.tblEmployees.Add(tblemployee);
db.SaveChanges();
}
catch (ArgumentException ae)
{
ModelState.AddModelError("", ae.Message);
}
return RedirectToAction("Index");
}
}
}
}
看法:
@model EmpiteHrSystem.Models.tblEmployee
@{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml";}
@Html.EditorFor(model => model.EmployeeId)
@Html.ValidationMessageFor(model => model.EmployeeId)
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@*@Html.EditorFor(model => model.Office)*@
@Html.DropDownList("DivisionOptions")
@Html.ValidationMessageFor(model => model.Division)
@Html.ActionLink("Back to List", "Index")