Hello在我的项目中,我的数据在使用EF时没有插入数据库。它是一个ASP.Net MVC3 Razor项目。 所以我试着调试程序,但它没有工作。当我们点击保存按钮时,它只是重新加载页面。
这里使用EF代码保存数据。
查看代码
<p>Welcome To @ViewBag.User</p>
@using (Html.BeginForm())
{
<fieldset class="default-fieldset">
<div></div>
<legend>Employee Registration Form</legend>
<div class="form-group">
<label class="col-lg-2 control-label">First Name</label>
<div class="col-lg-10" >@Html.TextBoxFor(model => model.FirstName, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">Middle Name</label>
<div class="col-lg-10" >@Html.TextBoxFor(model => model.MiddleName, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">Last Name</label>
<div class="col-lg-10" >@Html.TextBoxFor(model => model.LastName, new { @class ="form-control" })</div>
<label class="col-lg-2 control-label">Age</label>
<div class="col-lg-10" >@Html.TextBoxFor(model => model.Age, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">Date Of Birth</label>
<div class="col-lg-10" >@Html.TextBoxFor(model => model.DateOfBirth, new { @class = "form-control",@id="dp1" })</div>
<label class="col-lg-2 control-label">Address</label>
<div class="col-lg-10" >@Html.TextAreaFor(model => model.Address, new { @class = "tinymce-simple span12",@row="25",@cols="45",@width="30%" })</div>
<label class="col-lg-2 control-label">Position</label>
<div class="col-lg-10" >@Html.TextBoxFor(Model => Model.Position, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">Department</label>
<div class="col-lg-10" >@Html.TextBoxFor(Model => Model.Department, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">Date of joining</label>
<div class="col-lg-10" >@Html.TextBoxFor(model => model.DateOfJoining, new { @class = "form-control",@id="dp2" })</div>
<label class="col-lg-2 control-label">Qualification</label>
<div class="col-lg-10" >@Html.TextBoxFor(Model => Model.EducationalQuali, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">Experience :If any</label>
<div class="col-lg-10" >@Html.TextBoxFor(Model => Model.Experience, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">Others</label>
<div class="col-lg-10" >@Html.TextBoxFor(Model => Model.Others, new { @class = "form-control" })</div>
<div class="Button-Div-Sepa"></div>
<div class="Button-Div">
@*<input type="button" class="" />*@
<button type="submit" class="btn btn-success"> @Html.ActionLink("Save","EmployeeRegistration","Employee")</button>
<button class="btn btn-success">Clear</button>
<button class="btn btn-success">Cancel</button>
</div>
</div>
</fieldset>
}
</div>
控制器代码
[HttpPost]
public ActionResult EmployeeRegistration(Employee emp)
{
if (ModelState.IsValid)
{
db.EmpData.Add(emp);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View();
}
模型
[Table("tbl_Employee")]
public class Employee
{
[Key]
public int EmpId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public int Age{get;set; }
public DateTime DateOfBirth { get; set; }
public string Address { get; set; }
public string Position { get; set; }
public string Department { get; set; }
public DateTime DateOfJoining { get; set; }
public string EducationalQuali { get; set; }
public string Experience { get; set; }
public string Others { get; set; }
数据库上下文
public class ElixirERPContext :DbContext
{
public DbSet<Menus> DataMenu { get; set; }
public DbSet<Employee> EmpData { get; set; }
}
答案 0 :(得分:1)
试试这个
db.EmpData.AddObject(emp);
db.SaveChanges();
db.AcceptAllChanges();
更新了答案,注释中描述了问题:
表示您的db and entity model db is different, first update entity model
然后尝试保存