How do I redirect within a ViewResult or ActionResult function?

后端 未结 1 1543
栀梦
栀梦 2021-01-17 09:40

Say I have:

public ViewResult List() 
{
    IEnumerable myList = repository.GetMyList();
    if(1 == myList.Count())
    {
        RedirectToAc         


        
相关标签:
1条回答
  • 2021-01-17 10:15

    You need to return RedirectToAction instead of just calling the RedirectToAction method. Also, your method will need to return an ActionResult is a return type compatible with both ViewResult and RedirectToRouteResult.

    public ActionResult List() 
    {
        IEnumerable<IModel> myList = repository.GetMyList();
        if(1 == myList.Count())
        {
            return RedirectToAction("Edit", new { id = myList.Single().id });
        }
    
        return View(myList);
    }
    
    0 讨论(0)
提交回复
热议问题