Search code examples
asp.net-mvcdatabaseentity-framework-6crud

ASP.NET MVC database table getting null value


I am creating a simple CRUD application using ASP.NET MVC with Entity Framework, Data are being saved but the values are null when I check it from Database ..

Below I shared different class and files:-

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

Solution

  • Your view contains a @Html.HiddenFor() for each property before the associated EditorFor() method. The DefaultModelBinder only binds the first matching name/value pair and ignores the others, so its the values of the hidden inputs (which are default values) that are being saved.

    Remove all the @Html.HiddenFor() from your view and the edited values will be correctly bound.

    As a side note, its unclear why your method is named AddOrEdit when all you are doing is adding new records.