asp.net-mvc Asp.Net MVC 将对象从视图中的 Url.Action 传递到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6863501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asp.Net MVC Passing an object from Url.Action in View to Controller
提问by Bohemian
While there appears to be similar questions I do not believe I found one where somebody was passing the entire model from view to controller, the closest I found was passing a data value in the model to the controller... which is not what I want to do...
虽然似乎有类似的问题,但我不相信我发现有人将整个模型从视图传递到控制器的问题,我发现最接近的是将模型中的数据值传递给控制器......这不是我想要的去做...
I want to pass the model object to the applicable controller action result as a parameter and do what I want to do with the data in the model object from within the controller...
我想将模型对象作为参数传递给适用的控制器操作结果,并从控制器内部对模型对象中的数据执行我想做的操作...
code abbreviated...
代码缩写...
in the view i am using Url.Action(string, string, object) to pass the controller method name, and the route to it as the 2nd parameter, and the entire model object as the 3rd parameter...
在视图中,我使用 Url.Action(string, string, object) 传递控制器方法名称,并将其路由作为第二个参数,将整个模型对象作为第三个参数...
my third parameter is the problem, the Url.Action(string, string) over load works fine with the same values, so I don't have any problem getting to the controller action result method, with the values I am using...
我的第三个参数是问题,Url.Action(string, string) 过载在相同的值下工作正常,所以我没有任何问题进入控制器操作结果方法,使用我正在使用的值......
view code snippet...
查看代码片段...
$('#ControllerActionResult').click(function (e) {
window.location = '<%: Url.Action("ControllerActionResult", TheRoute, new { pModel = Model } ) %>';
});
controller code snippet...
控制器代码片段...
public ActionResult ControllerActionResult(object pModel)
{
}
My error is "CS1502: The best overloaded method match for 'System.Web.Mvc.UrlHelper.Action(string, string, object)' has some invalid arguments" and the window location line is highlighted...
我的错误是“CS1502:'System.Web.Mvc.UrlHelper.Action(string, string, object)'的最佳重载方法匹配有一些无效参数”并且突出显示了窗口位置行...
Am I missing something obvious?
我错过了一些明显的东西吗?
回答by Andy
I don't think you can do that. Url is trying to build a url, not call the action method directly. You can pass individual key value pairs via the anonymous object, which Url can then use to build a link. Model binding can take care of putting the key value pairs into the incoming model for you.
我不认为你可以这样做。url 正在尝试构建一个 url,而不是直接调用 action 方法。您可以通过匿名对象传递单个键值对,然后 Url 可以使用它来构建链接。模型绑定可以为您将键值对放入传入模型中。
Also, is TheRoute a string as well?
另外,TheRoute 也是字符串吗?
If you need a Url you don't have many alternatives. Either pass all the values to rebuild the model, or just pass a key value to retreive the model from the database again (or maybe session / viewdata / tempdata, if you're using that).
如果您需要一个网址,您没有很多选择。要么传递所有值来重建模型,要么只传递一个键值来再次从数据库中检索模型(或者可能是 session/viewdata/tempdata,如果你正在使用它)。
回答by Tae-Sung Shin
There might be several issues in your code. But I will answer what you want to know, how to send data to controller from js. You have to first send "model" in json format, possibly with jquery.ajax. For example,
您的代码中可能有几个问题。不过,我会回答你想知道,如何将数据从JS发送到控制器什么。您必须首先以 json 格式发送“模型”,可能使用 jquery.ajax。例如,
$.ajax({
url: '@Url.Action("ControllerActionResult")',
type: 'POST',
data: JSON.stringify(model),
dataType: 'json',
processData: false,
contentType: 'application/json; charset=utf-8',
success: OnSuccess
});
In controller, you have to have a defined class for the "model" and that should be the parameter of your controller.
在控制器中,您必须为“模型”定义一个类,这应该是控制器的参数。
[HttpPost]
public ActionResult ControllerActionResult(Model model) {
if (Request.IsAjaxRequest()) {
//to do
}
}
Otherwise, MVC cannot find a right controller to call.
否则,MVC 找不到合适的控制器来调用。
回答by sushil
try with following code it works for me
尝试使用以下代码对我有用
Javascript:
Javascript:
var location ='@url,action(string ,obj)'; window.location=location;
Controller:
控制器:
public ActionResult index(string obj){return view();}
hope this works for you.
希望这对你有用。

