return url.action as json object mvc

2019-08-09 04:49发布

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

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.

snapshot from inspect element

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?

Thanks,

2条回答
欢心
2楼-- · 2019-08-09 05:34

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.

查看更多
Juvenile、少年°
3楼-- · 2019-08-09 05:48

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

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

return Json(url , JsonRequestBehavior.AllowGet);
查看更多
登录 后发表回答