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

时间:2018-06-13 07:30:29

标签: asp.net-mvc database entity-framework-6 crud

我正在使用带有实体框架的ASP.NET MVC创建一个简单的CRUD应用程序,正在保存数据但是当我从数据库中检查它时值为null。

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

EmployeeController类:

Index,Model,lat,long
0,X,1.3539,103.84
1,X,1.3545,103.84
2,Y,1.3839,103.7002
3,X,1.3548,103.84
4,Y,1.3831,103.71
5,Z,1.3139,103.88

AddOrEdit.cshtml

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

        }
    }
}

Employee.cs

@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>
}

2 个答案:

答案 0 :(得分:2)

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

从视图中删除所有AddOrEdit,编辑后的值将被正确绑定。

作为旁注,当您所做的只是添加新记录时,不清楚为什么您的方法被命名为onClick="/myFunction( this )"

答案 1 :(得分:0)

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