2014-10-01 54 views
-1

我希望在删除完成后返回索引,但在索引中,我有返回一些Id的类别列表。所以问题是如何使用CategoryId在Index中返回?如何在调用删除方法后返回到控制器的索引

这里是指数:

public ActionResult Index([Bind(Prefix = "Id")] int categoryId) 
    { 
     var category = _db.Categories.Find(categoryId); 
     if (category != null) 
     { 
      return View(category); 
     } 
     return HttpNotFound(); 
    } 

和删除:

public ActionResult DeleteConfirmed(int id) 
    { 
     var entry = _db.Entries.Single(r => r.Id == id); 

     _db.Entries.Remove(entry); 

     _db.SaveChanges(); 

     return RedirectToAction("Index"); 
    } 

回答

1

Index方法的参数是categoryId所以

return RedirectToAction("Index", new { categoryId = entry.FK_CategoryId});` 

注意,你不需要的[Bind(Prefix = "Id")]

public ActionResult Index(int categoryId) 
+0

谢谢您的回答,我已经解决了这个问题,我没有删除'[绑定(PREFIX =“ Id“)],只是改变了一点逻辑,因为我错了。 – McKeymayker 2014-10-01 11:08:42

+0

我不认为你的理解'[绑定(前缀...)它在POST方法中用于复杂属性[本文](http://www.brainthud.com/cards/5218/19788/what-is-在aspnet-mvc-and-how-is-it-used-bindingText-in-the-context-of-aspnet-mvc-and-how-it-used)可能有帮助 – 2014-10-01 11:29:53

+0

是的,你是对的,我是初学者,我还在学习。文章 – McKeymayker 2014-10-01 12:33:18

1

我已经找到解决方案,删除方法现在看起来是这样的:

public ActionResult Delete(int id = 0) 
    { 
     Entry entry = _db.Entries.Single(r => r.Id == id); 
     if (entry == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(entry); 
    } 

    // 
    // POST: /Restaurant/Delete/5 

    [HttpPost, ActionName("Delete")] 
    public ActionResult DeleteConfirmed(int id) 
    { 
     var entryToDelete = _db.Entries.Single(r => r.Id == id); 

     _db.Entries.Remove(entryToDelete); 

     _db.SaveChanges(); 

     return RedirectToAction("Index",new { id = entryToDelete.CategoryId }); 
    } 
相关问题