ASP.NET MVC 数据库 table 获取空值

ASP.NET MVC database table getting null value

我正在使用 ASP.NET MVC 和 Entity Framework 创建一个简单的 CRUD 应用程序,正在保存数据,但是当我从数据库中检查它时,值为空..

下面我分享了不同的 class 和文件:-

雇员控制器class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASP.NETMVCCRUD.Models;
using System.Data.Entity.Validation;

namespace ASP.NETMVCCRUD.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetData()
        {
            using (DBModel db = new DBModel())
            {
                List<Employee> emplist = db.Employees.ToList<Employee>();
                return Json(new { data = emplist }, JsonRequestBehavior.AllowGet);
            }

        }

        [HttpGet]
        public ActionResult AddOrEdit(int id=0) {

            return View(new Employee());

        }

        [HttpPost]
        public ActionResult AddOrEdit(Employee emp)
        {
            using (DBModel db = new DBModel())
            {
                db.Employees.Add(emp);

                db.SaveChanges();

                return Json(new {success = true, message="Saved Successfully",JsonRequestBehavior.AllowGet });
            }

        }
    }
}

AddOrEdit.cshtml

@model ASP.NETMVCCRUD.Models.Employee
@{
    Layout = null;
}

@using (Html.BeginForm("AddOrEdit", "Employee", FormMethod.Post, new {onsubmit="return SubmitForm(this)" }))
{ 

    @Html.HiddenFor(Model => Model.EmployeeID)
    <div class="form-group">
        @Html.HiddenFor(Model => Model.Name, new { @class = "control-label" })
        @Html.EditorFor(Model => Model.Name, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(Model => Model.Name)
    </div>
    <div class="form-group">
        @Html.HiddenFor(Model => Model.Position, new { @class = "control-label" })
        @Html.EditorFor(Model => Model.Position, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(Model => Model.Position)
    </div>
    <div class="form-group">
        @Html.HiddenFor(Model => Model.Office, new { @class = "control-label" })
        @Html.EditorFor(Model => Model.Office, new { htmlAttributes = new { @class = "form-control" } })
    </div>
    <div class="form-group">
        @Html.HiddenFor(Model => Model.Age, new { @class = "control-label" })
        @Html.EditorFor(Model => Model.Age, new { htmlAttributes = new { @class = "form-control" } })
    </div>
    <div class="form-group">

        @Html.HiddenFor(Model => Model.Salary, new { @class = "control-label" })
    <div class="input-group">
        <span class="input-group-addon">$</span>
        @Html.EditorFor(Model => Model.Salary, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">

        <input type="submit" value="Submit" class="btn btn-primary"/>
        <input type="reset" value="Reset" class="btn " />
    </div>
}

Employee.cs

namespace ASP.NETMVCCRUD.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Employee
    {
        public int EmployeeID { get; set; }

        public string Name { get; set; }

        public string Position { get; set; }
        public string Office { get; set; }
        public Nullable<int> Age { get; set; }
        public Nullable<int> Salary { get; set; }
    }
}

您的视图在关联的 EditorFor() 方法之前为每个 属性 包含一个 @Html.HiddenFor()DefaultModelBinder 仅绑定第一个匹配的 name/value 对并忽略其他对,因此它是被保存的隐藏输入的值(默认值)。

从您的视图中删除所有 @Html.HiddenFor(),编辑后的值将被正确绑定。

附带说明一下,当您所做的只是添加新记录时,不清楚为什么您的方法被命名为 AddOrEdit

[HttpPost]

    public ActionResult AddOrEdit(Employee emp)
    {
        using (DBModel db = new DBModel())
        {
            db.Employees.Add(emp);
            try
            {
                db.SaveChanges();
            }
            catch(DbEntityValidationException e)
            {
                Console.WriteLine(e);
            }
            return Json(new { success = true, message = "Saved Succesfully" }, JsonRequestBehavior.AllowGet);
        }
            
    }