0

我想保存我的视图类数据,但我单击创建按钮,它没有保存在数据库中,如果有人知道原因,请帮助我解决这个问题。谢谢。

我的观点

@model EmpiteHrSystem.Models.tblEmployee
@{
ViewBag.Title = "Create";}

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.EmployeeId)
@Html.HiddenFor(model => model.DepartmentId)
<input type="submit" value="Create" class="linkButton"/> 
@Html.ActionLink("Cancel", "Index",null,new { @class = "linkButton" })

 @Html.DropDownList("TitleOptions","Select Department")
 @Html.ValidationMessageFor(model => model.Title)

我的控制器类

public ActionResult Create()
{
   ViewBag.TitleOptions = new SelectList(db.tblEmployees, "EmployeeId", "Title","123M");
   ViewBag.DivisionOptions = new SelectList(db.tblDepartments, "DepartmentId", "DepartmentType");
   ViewBag.PositionOptions = new SelectList(db.tblEmployees, "EmployeeId", "Position");
   ViewBag.OfficeOptions = new SelectList(db.tblDepartments, "DepartmentId", "DepartmentLocation");
   //ViewBag.Position = new SelectList(db.tblDepartments, "DepartmentId", "DepartmentType", "123M");
   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");
    }

    ViewBag.DepartmentId = new SelectList(db.tblDepartments, "DepartmentId", "DepartmentType", tblemployee.DepartmentId);

    return View(tblemployee);
}

我的模特班

public partial class tblEmployee
{
    //[HiddenInput(DisplayValue = false)]
    //[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int EmployeeId { get; set; }

    //[Required()]
    public string Title { get; set; }

    //[Required()]
    //[RegularExpression(@"^\d{3}-?\d{3}-?\d{4}$")]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    //[Required()]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    //[Required()]
    //[DataType(DataType.Date)]
    [Display(Name = "Date of Birth")]
    public Nullable<System.DateTime> DateOfBirth { get; set; }

    //[Required()]
    //[DataType(DataType.Date)]
    [Display(Name = "Date of Join")]
    public Nullable<System.DateTime> DateOfJoin { get; set; }

    //[Required()]
    //[Display(Name = "Position")]
    public string Position { get; set; }

    //[Required()]
    //[ForeignKey("tblDepartment")]
    //[HiddenInput(DisplayValue = false)]
    public string DepartmentId { get; set; }

    //[Required()]
    //[DataType(DataType.Currency)]
    public Nullable<decimal> Salary { get; set; }

    public virtual tblDepartment tblDepartment { get; set; }
}
4

0 回答 0