【问题标题】:ASP.NET MVC database table getting null valueASP.NET MVC 数据库表获取空值
【发布时间】:2018-06-13 07:30:29
【问题描述】:

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

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

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

        }
    }
}

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

【问题讨论】:

  • 为什么是htmlAttribute onsubmit="return SubmitForm(this) ??

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 嗨,欢迎 :) 简短的文字介绍/解释可能会使此答案对其他人更有帮助
    • 欢迎来到 Stack Overflow!虽然此代码可能会解决问题,但包括解释如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案添加解释并说明适用的限制和假设。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多