[英]ASP.NET Core Redirect with data object
I'm making a Action method in a controller like following. 我在如下所示的控制器中制作一个Action方法。
In Model 在模型中
public class Model
{
public string modelStr { get; set; }
}
In Controller 在控制器中
public IActionResult ActionMethod1(Model value)
{
return View(value); ---- debug point (1)
}
public IActionResult ActionMethod2(int pageKind)
{
Model temp = new Model();
if (pageKind == 1)
{
temp.modelStr = "Hi ASP.NET Core";
}
else
{
temp.modelStr = "error!";
}
return RedirectToAction("ActionMethod1", temp); ---- debug point (2)
}
And I access ActionMethod2 with /Controller/ActionMethod2
in browser. 然后在浏览器中使用
/Controller/ActionMethod2
访问ActionMethod2。 I expected /Controller/ActionMethod1
will be called and its model value
has value which is sent by ActionMethod2
. 我期望
/Controller/ActionMethod1
将被调用,并且其模型value
具有由ActionMethod2
发送的值。
But if I debug it, 1) debuger do not stop at debug point (2)
and at debug point (1)
the value
has no value, ie value.modelStr == null
. 但是,如果我对其进行调试,则1)
debug point (2)
不会在debug point (2)
和debug point (1)
处停止,该value
没有值,即value.modelStr == null
。
How do value at debug point (1)
get a value from ActionMethod2
? debug point (1)
的值如何从ActionMethod2
获取值?
This is caused by parameter binding . 这是由参数绑定引起的。
The parameter value
of ActionMethod1
is type of Model
class, this is a complex type, mvc will try to bind it's value from http body, the http request should be a POST request. ActionMethod1
的参数value
是Model
类的类型,这是一个复杂类型,mvc将尝试从http正文绑定其值,http请求应该是POST请求。
But ActionMethod2
returns a RedirectToAction
result, the browser redirect to ActionMethod1
with GET
, and can not handle by ActionMethod1
. 但是
ActionMethod2
返回一个RedirectToAction
结果,浏览器使用GET
重定向到ActionMethod1
,并且不能由ActionMethod1
处理。
The answer is add FromQueryAttribute
to the parameter value
of ActionMethod1
, tells mvc that value
is bind from query string, like this: 答案是将
FromQueryAttribute
添加到ActionMethod1
的参数value
,告诉mvc value
是从查询字符串绑定的,如下所示:
public IActionResult ActionMethod1([FromQuery]Model value)
{
return View(value); ---- debug point (1)
}
Now ActionMethod1
will handle GET request, and ActionMethod2
can redirect to it. 现在,
ActionMethod1
将处理GET请求,而ActionMethod2
可以将其重定向到该请求。
What you're trying to do is not possible. 您试图做的事是不可能的。 However there are work-arounds.
但是,有变通办法。 One common approach is to add the data to the TempData dictionary and retrieve it in the other method:
一种常见的方法是将数据添加到TempData字典中,并通过另一种方法进行检索:
public IActionResult ActionMethod1()
{
var value = (Model)TempData["model"];
return View(value);
}
public IActionResult ActionMethod2(int pageKind)
{
Model temp = new Model();
if (pageKind == 1)
{
temp.modelStr = "Hi ASP.NET Core";
}
else
{
temp.modelStr = "error!";
}
TempData["model"] = temp;
return RedirectToAction("ActionMethod1");
}
Alternatively, are you sure you even need to actually redirect the user? 另外,您确定甚至需要实际重定向用户吗? You could always just call the method directly:
您总是可以直接直接调用该方法:
public IActionResult ActionMethod1(Model value)
{
return View(value);
}
public IActionResult ActionMethod2(int pageKind)
{
Model temp = new Model();
if (pageKind == 1)
{
temp.modelStr = "Hi ASP.NET Core";
}
else
{
temp.modelStr = "error!";
}
return ActionMethod1(temp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.