Я создаю простое приложение CRUD с использованием ASP.NET MVC с Entity Framework. Данные сохраняются, но при проверке их из базы данных значения равны нулю.
Ниже я поделился другим классом и файлами:-
Класс 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; }
}
}