[英]Redirecting to another action in same controller and passing some data asp.net mvc3
I want to redirect to another action in same controller and pass one parameter value. 我想重定向到同一控制器中的另一个动作并传递一个参数值。 I have this code:
我有以下代码:
public ActionResult Index()
{
}
public ActionResult SomeAction()
{
if (isFailed)
{
// Redirect to Index Action with isFailed parameter so that some message gets displayed on Index page.
}
}
I read about using TempData
but my session is read only (for some purpose) thus I if I save data in TempData
in SomeAction
, it does not help since TempData
is empty in Index
action. 我读过有关使用
TempData
但是我的会话是只读的(出于某种目的),因此,如果我将数据保存在TempData
中的SomeAction
, TempData
SomeAction
帮助,因为Index
动作中的TempData
为空。
Another thing I tried is using RedirectToAction("Index","Test", new { param = isFailed})
in return
statement of SomeAction
. 我尝试的另一件事是在
SomeAction
return
语句中使用RedirectToAction("Index","Test", new { param = isFailed})
。 This works and I can access param
in Index
action using Request.QueryString['param']
but the issue with this is the url now becomes /AreaName?param=true
where I want it to be /AreaName/Test/
. 这工作,我可以访问
param
在Index
使用动作Request.QueryString['param']
但这个问题是url现在变为/AreaName?param=true
,我希望它是/AreaName/Test/
。
This is my routemap: 这是我的路线图:
context.MapRoute(
"default",
"AreaName/{controller}/{action}/{param}",
new { controller="Test", action = "Index", param=UrlParameter.Optional },
);
I am submitting a form using "post" from MyJS.js to SomeAction
. 我正在使用MyJS.js中的“ post”向SomeAction提交
SomeAction
。
Is there any alternative/workaround method for doing this? 是否有其他替代方法/解决方法? In nutshell, I want these three things:
简而言之,我想要这三件事:
Index
action. Index
操作。 param
from SomeAction
to Index
. param
的值从SomeAction
给Index
。 http://localhost/AreaName/Test/
http://localhost/AreaName/Test/
Try this 尝试这个
public ActionResult Index(Datatype param)
{
}
public ActionResult SomeAction()
{
if (isFailed)
{
return RedirectToAction("Index" , "Home",new{param= value });
}
return View();
}
Index
action. Index
操作。 Use return RedirectToAction("Index","Home");
使用
return RedirectToAction("Index","Home");
SomeAction
to Index
. SomeAction
给Index
。 You can use the TempData object. 您可以使用TempData对象。
The TempData property value is stored in session state.
TempData属性值存储在会话状态中。 Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them.
设置TempDataDictionary值后调用的任何操作方法都可以从对象获取值,然后对其进行处理或显示。 The value of TempData persists until it is read or until the session times out.
TempData的值将一直保留,直到被读取或会话超时为止。
You can do it as below: 您可以按照以下步骤进行操作:
public ActionResult Index()
{
//you can access TempData here.
}
public ActionResult SomeAction()
{
if (isFailed)
{
TempData["Failure"] = "Oops, Error"; //store to TempData
return RedirectToAction("Index" , "Home");
}
return View();
}
This MSDN article explains it all. 这篇MSDN文章解释了这一切。
http://localhost/AreaName/Test/
http://localhost/AreaName/Test/
You can do so by adding a route in your RouteConfig.cs
您可以通过在
RouteConfig.cs
添加路由来RouteConfig.cs
context.MapRoute(
"default",
"AreaName/Test/",
new { area = "AreaName" controller="Test", action = "Index"},
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.