Asp.net mvc ASP.NET MVC数据库表获取空值

Asp.net mvc ASP.NET MVC数据库表获取空值,asp.net-mvc,database,entity-framework-6,crud,Asp.net Mvc,Database,Entity Framework 6,Crud,我正在使用ASP.NET MVC和Entity Framework创建一个简单的CRUD应用程序,数据正在保存,但当我从数据库中检查时,值为空 下面我共享了不同的类和文件:- EmployeeController类: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ASP.NETMVCCRUD.Models; using

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

下面我共享了不同的类和文件:-

EmployeeController类:

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 });
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用ASP.NETMVCCRUD.Models;
使用System.Data.Entity.Validation;
命名空间ASP.NETMVCCRUD.Controllers
{
公共类EmployeeController:控制器
{
//获取:员工
公共行动结果索引()
{
返回视图();
}
公共操作结果GetData()
{
使用(DBModel db=new DBModel())
{
List employist=db.Employees.ToList();
返回Json(新的{data=emplist},JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
公共操作结果AddOrEdit(int id=0){
返回视图(新员工());
}
[HttpPost]
公共行动结果AddOrEdit(员工环境管理计划)
{
使用(DBModel db=new DBModel())
{
db.Employees.Add(环境管理计划);
db.SaveChanges();
返回Json(新的{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>
}
@model ASP.NETMVCCRUD.Models.Employee
@{
布局=空;
}
@使用(Html.BeginForm(“AddOrEdit”,“Employee”,FormMethod.Post,new{onsubmit=“return SubmitForm(this)”}))
{ 
@Html.HiddenFor(Model=>Model.EmployeeID)
@Html.HiddenFor(Model=>Model.Name,新的{@class=“control label”})
@EditorFor(Model=>Model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(Model=>Model.Name)
@Html.HiddenFor(Model=>Model.Position,新的{@class=“control label”})
@EditorFor(Model=>Model.Position,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(Model=>Model.Position)
@Html.HiddenFor(Model=>Model.Office,新的{@class=“control label”})
@EditorFor(Model=>Model.Office,new{htmlAttributes=new{@class=“form control”})
@Html.HiddenFor(Model=>Model.Age,新的{@class=“control label”})
@EditorFor(Model=>Model.Age,new{htmlAttributes=new{@class=“form control”})
@Html.HiddenFor(Model=>Model.Salary,新的{@class=“control label”})
$
@EditorFor(Model=>Model.Salary,new{htmlAttributes=new{@class=“form control”})
}
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; }
    }
}
名称空间ASP.NETMVCCRUD.Models
{
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
公共部分类雇员
{
public int EmployeeID{get;set;}
公共字符串名称{get;set;}
公共字符串位置{get;set;}
公共字符串办公室{get;set;}
公共可空年龄{get;set;}
公共可空薪资{get;set;}
}
}

对于关联的
EditorFor()
方法之前的每个属性,视图都包含一个
@Html.HiddenFor()
DefaultModelBinder
仅绑定第一个匹配的名称/值对,而忽略其他名称/值对,因此它是正在保存的隐藏输入(默认值)的值

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


作为旁注,不清楚为什么您的方法命名为
AddOrEdit
,而您所做的只是添加新记录。

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

从视图中删除所有
@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);
        }
            
    }
[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);
        }
            
    }

为什么是这个
htmlAttribute
onsubmit=“return SubmitForm(this)
?为什么是这个
htmlAttribute
onsubmit=“return SubmitForm(this)
?您好,欢迎:)一个小的文本介绍/解释可能会让这个答案对其他人更有帮助,来看看堆栈溢出!虽然这段代码可能会解决这个问题,包括如何以及为什么解决这个问题的解释将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在您的答案中添加解释,并说明适用的限制和假设。您好,欢迎:)一个小的文本介绍/解释可能会使此答案对其他人更有用。来看看堆栈溢出!虽然这段代码可能会解决这个问题,包括如何以及为什么解决这个问题的解释将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。