这是家庭作业,一个ASP.NET MVC和Web表单Web应用程序使用存储库(数据硬编码,无数据库)对医生进行评分。用户应该能够编辑和删除条目,但Controller上的“删除”链接不会进入“删除”页面。但是,当我将鼠标悬停在“删除”链接上时,URI是:DoctorApplication/Delete?DoctorPicture = Images/0.cropped.jpg。我在整个应用程序中使用字符串DoctorPicture而不是int ID,并且我正在考虑路径Images/0.cropped而不是0.cropped,而没有Images目录是为什么我的Delete视图没有显示,但在我的TestDoctorRepository中,我有DoctorPicture =“Images/0cropped.jpg”,以便图像可以显示在页面上。为什么不显示“删除”视图?控制器的“删除”链接没有进入“删除”页面
public TestDoctorRepository()
{
doctors = new List<Doctor> {
new Doctor { DoctorPicture = "Images/0cropped.jpg", DoctorName = "Michael Shores", DoctorSpecialty = "Opthamology", times = 0, rating = 0, rated = true, avg=0, fave = true },
// more code
};
}
public void Remove(string DoctorPicture)
{
var doctor = from d in doctors where d.DoctorPicture == DoctorPicture select d;
doctors.Remove(doctor.FirstOrDefault());
}
从DoctorApplicationController:
public ActionResult Delete(string DoctorPicture)
{
repo.Remove(DoctorPicture);
return RedirectToAction("Index");
}
//private ActionResult View(Func<List<Doctor>> func)
//{
// throw new NotImplementedException();
//}
//
// POST: /DoctorApplication/Delete/5
[HttpPost]
public ActionResult Delete(string DoctorPicture, FormCollection collection)
{
try
{
// TODO: Add delete logic here
repo.Remove(DoctorPicture);
return RedirectToAction("Index");
}
catch
{
return View();
}
我将Delete方法更改为: public ActionResult Delete(string DoctorPicture) { repo.Remove(DoctorPicture); return RedirectToAction(“Delete”); } 和 [HttpPost] 公众的ActionResult删除(字符串DoctorPicture,收集的FormCollection) { 尝试 {// TODO:添加删除逻辑这里 repo.Remove(DoctorPicture); return RedirectToAction(“Delete”); } catch { return View(); } } 但是当我运行它时...... – Grafica
有错误表示“Firefox已经检测到服务器正以一种永远不会完成的方式重定向该地址的请求。” – Grafica
这是因为你不能将它重定向到它自己。你使用了错误的return语句。 'RedirectToAction'与调用控制器中的特定方法基本相同。因此,具有'RedirectToAction(“Delete”)“的'Delete'方法永远不会结束,因为它会继续调用它自己。你需要像'return View(“Delete”);' – IronMan84