简体   繁体   English

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

[英]return url.action as json object mvc

My Controller method returns Json object which has url formed as shown below :我的控制器方法返回 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.在 ajax 调用的成功代码中,我想将此 url 分配给 window.location.href 以便我可以根据形成的 url 重定向查看。 But return value of ajax call shows plain text value for route value passed as part of url.但是ajax调用的返回值显示作为url的一部分传递的路由值的纯文本值。

来自检查元素的快照

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?那么,为了传递我的路由值(包括集合在内的复杂 c# 对象),我有哪些选项? Is there any better approach to achieve what I want to do?有没有更好的方法来实现我想做的事情?

Thanks,谢谢,

You can pass the url string directly instead of 'Url.Action()'.您可以直接传递 url 字符串而不是 'Url.Action()'。 Something like this:像这样的东西:

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

return Json(url , JsonRequestBehavior.AllowGet);

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.当然,我从来没有测试过这个。

Simple and short简单而简短

return Json(new { redirectUrl = @Url.Action("View", "Controller") }); return Json(new { redirectUrl = @Url.Action("View", "Controller") });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM