如何重定向到与其他动作参数的动作不传递参数? [英] How to redirect to an action with parameters from other action without passing parameters?

查看:147
本文介绍了如何重定向到与其他动作参数的动作不传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,在CreateTest里,uponsuccessful,我想重定向到来自的CreateTest测试。

我想要做类似如下:

 公众的ActionResult测试(INT ID,字符串项目名)
    {
        TestModel模式=新TestModel(ID,项目名);
        返回查看(模型);
    } [HttpPost]
    公众的ActionResult的CreateTest(TestModel模型)
    {
        尝试
        {
            返回RedirectToAction(测试);
        }
        赶上(例外五)
        {
            ModelState.AddModelError(错误,e.Message);
            返回查看(模型);
        }
    }


解决方案

您可能会重定向时需要提供的参数:

 返回RedirectToAction(测试,新{
   ID = model.ID,
   PROJECTNAME = model.ProjectName
});

,你会被重定向到现在这个样子的网址:

/美孚/测试ID = 123安培; PROJECTNAME = ABC

Below, in CreateTest, uponsuccessful, I want to redirect to Tests from CreateTest.

I want to do something like the following:

    public ActionResult Tests(int ID, string projectName)
    {
        TestModel model = new TestModel (ID, projectName);
        return View(model);
    }

 [HttpPost]
    public ActionResult CreateTest(TestModel model)
    {
        try
        {
            return RedirectToAction("Tests");
        }
        catch (Exception e)
        {
            ModelState.AddModelError("Error", e.Message);
            return View(model);
        }
    }

解决方案

You might need to provide the arguments when redirecting:

return RedirectToAction("Tests", new { 
   ID = model.ID, 
   projectName = model.ProjectName 
});

and the url you will be redirecting to will now look something like this:

/Foo/Tests?ID=123&projectName=abc

这篇关于如何重定向到与其他动作参数的动作不传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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