1

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,

5
  • What gets assigned to contactVM? Not sure what you mean by route value Commented May 13, 2015 at 14:14
  • contactVM is viewmodel object which I want to pass to my action method Commented May 13, 2015 at 14:36
  • What on earth is the point of making an ajax call if you then immediately redirect. Just do a normal submit. Commented May 13, 2015 at 22:00
  • And since contactVM is clearly a complex object, even if you used the correct syntax, this probably wont work anyway. You should not pass complex objects to a GET method. Apart from the ugly query string, if the model contains properties which are complex objects or collections, binding will fail and there is a chance you could exceed the query string limit and throw an exception Commented May 13, 2015 at 22:06
  • @StephenMuecke I totally agree with concerns in your comments. I know what I was trying to do is not good but I needed to be back on client side after server side call hence I thought of such implementation. Now I dropped my idea of such implementation and taking alternate way. Thanks for your inputs. Commented May 15, 2015 at 8:55

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

above solution works but results in very lengthy n nasty url... Thanks for your help.
0

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);

1 Comment

it doesn't work as suggested by you. It just adds up fully qualified name of my object type to url i.e. Customer/ShowContact?vm=<NameSpace>.ContactVM
0

Simple and short

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.