ASP.NET MVC database table getting null value

asp.net-mvc crud database entity-framework-6

Question

I'm using ASP.NET MVC and Entity Framework to build a simple CRUD application, however even though the data is being saved, the values are empty when I verify them against the database.

I've supplied some classes and files below:

Class 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; }
    }
}
1
0
6/13/2018 7:30:29 AM

Accepted Answer

ZZZ_tmp
2
6/13/2018 7:46:04 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow