[英]ASP.Net MVC3 Razor Data Insertion is not working when using EF
您好,在我的項目中,使用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; }
}
嘗試這個
db.EmpData.AddObject(emp);
db.SaveChanges();
db.AcceptAllChanges();
更新了答案作為注釋中描述的問題:
這意味着您的db and entity model db is different, first update entity model
然后嘗試保存
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.