我该如何重定向的ViewResult或ActionResult的函数内? [英] How do I redirect within a ViewResult or ActionResult function?

查看:150
本文介绍了我该如何重定向的ViewResult或ActionResult的函数内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有:

public ViewResult List() 
{
    IEnumerable<IModel> myList = repository.GetMyList();
    if(1 == myList.Count())
    {
        RedirectToAction("Edit", new { id = myList.Single().id });
    }

    return View(myList);
}

在这个函数中,我检查是否有只有一个列表项,如果有,我想直接重定向到处理该列表项的控制,否则我想显示的列表视图。

Inside this function, I check if there is only one item in the list, if there is I'd like to redirect straight to the controller that handles the list item, otherwise I want to display the List View.

我如何做到这一点?只需添加 RedirectToAction 不起作用 - 呼叫被击中,但VS只有几步之遥了它,并试图在底部返回查看。

How do I do this? Simply adding a RedirectToAction doesn't work - the call is hit but VS just steps over it and tries to return the View at the bottom.

推荐答案

您需要返回RedirectToAction ,而不是只调用RedirectToAction方法。另外,你的方法就需要返回一个的ActionResult 是一个返回类型既兼容的ViewResult RedirectToRouteResult

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);
}

这篇关于我该如何重定向的ViewResult或ActionResult的函数内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆