我有一个生成的视图,其中包含“待办事项”列表我正在尝试为生成的删除链接创建Delete方法。我创建了一个ListController,它已经处理了添加到列表数据库的问题。现在我试图让列表删除项目。这是我的ListController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyBasicListApp2.Models.DB;
using MyBasicListApp2.Models.EntityManager;
using System.Data.Entity;
namespace MyBasicListApp2.Controllers
{
public class ListController : Controller
{
// GET: List
public ActionResult Index()
{
return View();
}
// First I create a todolist action that is used to receive the default ToDoList page
// Second I create a post todolist action for submitting form info on the page.
// In the method I check to see if the filled out viewmodel contains valid model data state
// If data is valid then I create an EntityManager object (contains logic method) so I can use
// the addtolist method I made in the ListManager (EntityManager).
// Next I will authenticate the form and set an auth cookie for incase the current session does
// not include cookies.
public ActionResult ToDoList()
{
return View();
}
// In the post I pass in the viewmodel that the view will use to collect information
[HttpPost]
public ActionResult ToDoList(tblBasicList LVM)
{
if (ModelState.IsValid)
{
ListManager LM = new ListManager();
// Addtolist is a method belonging to ListManager and can only be called if ListManager
// object is created in the current context.
LM.AddToList(LVM);
return RedirectToAction("MyToDoList", "Home");
}
return View();
}
// Delete method
// First create the actionresult for the edit call
// Get method
public ActionResult Delete()
{
return View();
}
[HttpPost]
public ActionResult Delete(int BasicListID)
{
using (MyBasicListAppEntities db = new MyBasicListAppEntities())
{
ListManager LM = new ListManager();
LM.Remove(BasicListID);
return RedirectToAction("MyToDoList", "Home");
}
}
}
}
我有一个单独的类来处理我的实体框架方法,用于添加和删除:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyBasicListApp2.Models.DB;
namespace MyBasicListApp2.Models.EntityManager
{
public class ListManager
{
public void AddToList(tblBasicList LVM)
{
using (MyBasicListAppEntities db = new MyBasicListAppEntities())
{
tblBasicList TBL = new tblBasicList();
TBL.InputDate = LVM.InputDate;
TBL.InputItem = LVM.InputItem;
TBL.InputImportance = LVM.InputImportance;
TBL.ItemCompletion = LVM.ItemCompletion;
db.tblBasicLists.Add(TBL);
db.SaveChanges();
}
}
public void Remove(int id)
{
MyBasicListAppEntities db = new MyBasicListAppEntities();
tblBasicList objDelete = db.tblBasicLists.Find(id);
db.tblBasicLists.Remove(objDelete);
db.SaveChanges();
}
}
}
我从“MyToDoList.cshtml”视图开始:
@model IEnumerable<MyBasicListApp2.Models.DB.tblBasicList>
@{
ViewBag.Title = "MyToDoList";
}
<h2>MyToDoList</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.InputDate)
</th>
<th>
@Html.DisplayNameFor(model => model.InputItem)
</th>
<th>
@Html.DisplayNameFor(model => model.InputImportance)
</th>
<th>
@Html.DisplayNameFor(model => model.ItemCompletion)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.InputDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.InputItem)
</td>
<td>
@Html.DisplayFor(modelItem => item.InputImportance)
</td>
<td>
@Html.DisplayFor(modelItem => item.ItemCompletion)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.BasicListID }) |
@Html.ActionLink("Details", "Details", new { id=item.BasicListID }) |
@Html.ActionLink("Delete", "Delete", "List", new { id=item.BasicListID })
</td>
</tr>
}
</table>
从这里我选择列表中的一个项目(我现在大约有6个。add
方法工作正常)。但是,当我单击“删除”按钮时,出现“资源未找到”错误。它甚至不会重定向到删除视图,我很难弄清楚原因。目标是指向删除页面,点击所选项目的删除,然后重定向回“MyToDoList”页面。有什么想法吗?
答案 0 :(得分:1)
要获得简短的回答,请将Delete方法(对于HTTP GET)更改为
public ActionResult Delete(int id)
{
tblBasicList todoItem = FindTodoItem(id); //with whatever method to get that todo item
return View(todoItem);
}
我猜您正在尝试使用ASP.NET MVC创建一个Web应用程序。控制器/视图/模型有一些命名约定。我会在此链接http://www.asp.net/mvc/overview/getting-started/introduction/getting-started
中推荐一些教程供您参考答案 1 :(得分:0)
对于像Delete这样的Controller,你不需要Get和a [HttpPost],因为它没有实际的视图。
public ActionResult Delete(int BasicListID)
{
MyBasicListAppEntities db = new MyBasicListAppEntities();
tblBasicList objDelete = db.tblBasicLists.Find(id);
db.tblBasicLists.Remove(objDelete);//this can actually be simplified
return RedirectToAction("Home");
}
你只需要将它重定向到像家一样的控制器而不将列表传递给它,家庭控制器将负责传递列表。