将url.action作为json对象返回mvc

时间:2022-11-30 22:56:56

My Controller method returns Json object which has url formed as shown below :

我的Controller方法返回Json对象,其中url的形成如下所示:

return Json(new { url = Url.Action("ShowContact", "Customer", new { vm = contactVM }) }, JsonRequestBehavior.AllowGet);

In the ajax call's success code, I want to assign this url to window.location.href so I can redirect to view as per url formed. But return value of ajax call shows plain text value for route value passed as part of url.

在ajax调用的成功代码中,我想将此url分配给window.location.href,以便我可以根据url形成重定向到视图。但是ajax调用的返回值显示作为url的一部分传递的路由值的纯文本值。

将url.action作为json对象返回mvc

Hence, I'm not getting whatever route value I want to pass to the redirect action method of my controller.

因此,我没有得到任何我希望传递给控制器​​的重定向操作方法的路由值。

So what are the options I have in order to pass my route value which is complex c# object including collections? Is there any better approach to achieve what I want to do?

那么我有什么选择来传递我的路由值,这是复杂的c#对象,包括集合?有没有更好的方法来实现我想做的事情?

Thanks,

2 个解决方案

#1


Try piecing it together:

尝试将它拼凑在一起:

return Json(
    new
    {
        url = Url.Action("ShowContact", "Customer"),
        vm = contactVM
    },
    JsonRequestBehavior.AllowGet);

Then on the client:

然后在客户端:

var url = result.url + '?vm=' + JSON.stringify(result.vm);

Granted, I've never tested this.

当然,我从未测试过这个。

#2


You can pass the url string directly instead of 'Url.Action()'. Something like this:

您可以直接传递url字符串,而不是'Url.Action()'。像这样的东西:

string url = "Customer/ShowContact?vm=" + contactVM;

return Json(url , JsonRequestBehavior.AllowGet);

#1


Try piecing it together:

尝试将它拼凑在一起:

return Json(
    new
    {
        url = Url.Action("ShowContact", "Customer"),
        vm = contactVM
    },
    JsonRequestBehavior.AllowGet);

Then on the client:

然后在客户端:

var url = result.url + '?vm=' + JSON.stringify(result.vm);

Granted, I've never tested this.

当然,我从未测试过这个。

#2


You can pass the url string directly instead of 'Url.Action()'. Something like this:

您可以直接传递url字符串,而不是'Url.Action()'。像这样的东西:

string url = "Customer/ShowContact?vm=" + contactVM;

return Json(url , JsonRequestBehavior.AllowGet);