我需要在 asp.net MVC 4 中应用更新和删除
I need to apply update and delete in asp.net MVC 4
基本上我已经创建了一个 mvc 应用程序,它将使用从数据库 edmx.So 生成的 entity framework 对数据库数据执行 CRUD 操作,在添加 EDM 和配置数据库设置之后,我添加了一个名为 CRUD 的控制器启用了脚手架 read/write 操作视图并将模型和数据库上下文附加到 it.Now 一切都按预期工作直到添加新员工即 Create.The 当我 [=35] 时出现问题=] crud 控制器的索引页面,当我按下操作链接编辑或删除任何特定员工数据时,它根本找不到编辑和删除 cshtml 页面,而是显示错误 404 资源不是 found.Please帮我找出问题所在,我在下面发布了我的全部代码:
public class CRUDController : Controller
{
private TestDBEntities2 db = new TestDBEntities2();
public ActionResult Index()
{
return View(db.Emps.ToList());
}
public ActionResult Details(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Emp emp)
{
if (ModelState.IsValid)
{
db.Emps.Add(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp);
}
public ActionResult Edit(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Emp emp)
{
if (ModelState.IsValid)
{
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp);
}
public ActionResult Delete(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Emp emp = db.Emps.Find(id);
db.Emps.Remove(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
Index.cshtml
@model IEnumerable<MvcApplication2.Emp>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeID)
</th>
<th>
@Html.DisplayNameFor(model => model.Firstname)
</th>
<th>
@Html.DisplayNameFor(model => model.Lastname)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Project)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Firstname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Lastname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /*id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
Create.cshtml
@model MvcApplication2.Emp
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Emp</legend>
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmployeeID)
@Html.ValidationMessageFor(model => model.EmployeeID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Project)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Project)
@Html.ValidationMessageFor(model => model.Project)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Edit.cshtml
@model MvcApplication2.Emp
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Emp</legend>
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmployeeID)
@Html.ValidationMessageFor(model => model.EmployeeID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Project)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Project)
@Html.ValidationMessageFor(model => model.Project)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
解决方案资源管理器图片:
尝试添加控制器名称
@Html.ActionLink("Edit", "Edit", "CRUD", new {/*id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", "CRUD", new { /*id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", "CRUD", new { /*id=item.PrimaryKey */ })
在您的 ActionLinks 中传递 EmployeeID:
@Html.ActionLink("Edit", "Edit", new {id=item.EmployeeID }) |
@Html.ActionLink("Details", "Details", new {id=item.EmployeeID }) |
@Html.ActionLink("Delete", "Delete", new {id=item.EmployeeID })
基本上我已经创建了一个 mvc 应用程序,它将使用从数据库 edmx.So 生成的 entity framework 对数据库数据执行 CRUD 操作,在添加 EDM 和配置数据库设置之后,我添加了一个名为 CRUD 的控制器启用了脚手架 read/write 操作视图并将模型和数据库上下文附加到 it.Now 一切都按预期工作直到添加新员工即 Create.The 当我 [=35] 时出现问题=] crud 控制器的索引页面,当我按下操作链接编辑或删除任何特定员工数据时,它根本找不到编辑和删除 cshtml 页面,而是显示错误 404 资源不是 found.Please帮我找出问题所在,我在下面发布了我的全部代码:
public class CRUDController : Controller
{
private TestDBEntities2 db = new TestDBEntities2();
public ActionResult Index()
{
return View(db.Emps.ToList());
}
public ActionResult Details(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Emp emp)
{
if (ModelState.IsValid)
{
db.Emps.Add(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp);
}
public ActionResult Edit(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Emp emp)
{
if (ModelState.IsValid)
{
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp);
}
public ActionResult Delete(int id = 0)
{
Emp emp = db.Emps.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Emp emp = db.Emps.Find(id);
db.Emps.Remove(emp);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
Index.cshtml
@model IEnumerable<MvcApplication2.Emp>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeID)
</th>
<th>
@Html.DisplayNameFor(model => model.Firstname)
</th>
<th>
@Html.DisplayNameFor(model => model.Lastname)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Project)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Firstname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Lastname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /*id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
Create.cshtml
@model MvcApplication2.Emp
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Emp</legend>
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmployeeID)
@Html.ValidationMessageFor(model => model.EmployeeID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Project)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Project)
@Html.ValidationMessageFor(model => model.Project)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Edit.cshtml
@model MvcApplication2.Emp
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Emp</legend>
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmployeeID)
@Html.ValidationMessageFor(model => model.EmployeeID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Project)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Project)
@Html.ValidationMessageFor(model => model.Project)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
解决方案资源管理器图片:
尝试添加控制器名称
@Html.ActionLink("Edit", "Edit", "CRUD", new {/*id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", "CRUD", new { /*id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", "CRUD", new { /*id=item.PrimaryKey */ })
在您的 ActionLinks 中传递 EmployeeID:
@Html.ActionLink("Edit", "Edit", new {id=item.EmployeeID }) |
@Html.ActionLink("Details", "Details", new {id=item.EmployeeID }) |
@Html.ActionLink("Delete", "Delete", new {id=item.EmployeeID })