如何使用相同的控制器ActionResult重定向到不同的页面?

时间:2013-10-15 13:52:24

标签: c# html5 asp.net-mvc-4

我有两个使用相同ActionResult的视图。最初只有一个视图,但现在需要一个视图。

观点是 - “索引”& “公认” 索引

    [HttpGet]
    public ActionResult Index(string status, string message)
    {
        var InboxStatus = InboxStatus.New;

        if (!Enum.TryParse(status, out inboxStatus))
            inboxStatus = InboxStatus.New;

        var model = new InboxModel();
        model.Status = inboxStatus.ToString();

        model.InboxMailCount = GetInboxMailCount();
        model.InboxMailCount.Status = InboxStatus.ToString();

        @ViewBag.Message = message;

        return View(model);
    }

接受

       [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Accept(InboxModel model)
    {
        if (ModelState.IsValid)
        {
            var inboxmail = _repo.GetById(model.ID);

            inboxmail.Status = (int)ReferralStatus.Accepted;
            inboxmail.AcceptedByUserId = UserId;
            inboxmail.AcceptenceDateTime = DateTime.Now;

            _uow.SaveChanges();

            return RedirectToAction("Index", new { Message = "Accepted Successfully" });
        }

        return View(model.ID.ToString());
    }

ActionResult被称为“拒绝”, 问题是操作结果包含以下内容......

  [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Decline(InboxModel model)
    {
        if (ModelState.IsValid)
        {
            var InBox = _repo.GetById(model.ID);

            InBox.Status = (int)ReferralStatus.Declined;
            InBox.DeclinedByUserId = UserId;
            InBox.DeclinedDateTime = DateTime.Now;

            _uow.SaveChanges();


            return RedirectToAction("Index", new { Message = "Declined Successfully" });
        }

        return Accept(model.ID.ToString());
    }

因此,如果用户在任一页面上选择“拒绝”操作,他们将被重定向到“索引”,无论他们在“已接受”视图上执行操作。 合理,?我想将它们重新引导回他们来自的页面。

请注意,这是我目前正在为“不同行动”重新指向“已接受”的方式......

由于Index中的列表取决于“Status”属性... 索引视图..

ASP.Net MVC 4使用razor 2视图.....任何想法?

4 个答案:

答案 0 :(得分:0)

您可以使用类似ViewBag.ReturnUrl的内容在操作序列中存储最新的ActionName。

示例:

public ActionResult ActionA(){
 ViewBag.ReturnUrl = "ActionA";
 return View();
}

public ActionResult ActionB(){
 ViewBag.ReturnUrl = "ActionB";
 return View();
}

public ActionResult Declined(){
 return RedirectToAction(ViewBag.ReturnUrl);
}

答案 1 :(得分:0)

您可以尝试缓存或保存以前的网址,然后重新定向到它。

    public ActionResult Index()
    {
        var previousPage = System.Web.HttpContext.Current.Request.UrlReferrer;


        //Yourlogic

        RedirectToAction(previousPage);
    }

答案 2 :(得分:0)

只需在InboxModel中添加一个属性即可说明您的来源。 然后,在表单中,您可以放置​​隐藏的值。

顺便说一句,你是在没有传递Status参数的情况下重定向到Index ...我错了吗?

答案 3 :(得分:0)

得到了!!道歉,这不是一个直截了当的问题,因为有很多代码可以添加到混乱中,多亏了你所有的时间..

基本上在行动开始时保存邮件的初始状态。 因为状态只会在动作中发生变化。 在Accepted ActionResult的顶部,

 var returnStatus = Mail.Status;

然后在return ....中使用该变量。

  return RedirectToAction("Index", new { Status = (int)returnStatus, Message = "Mail Successfully added to Sent Listing" });