1

I am using ASP.NET MVC 3 for the first time. I want to call a controller action with a single parameter. this parameter is an object, not a simple type. Let's say: Controller = "Person", Action="Add", The single argument of this action is an object: "Person" = {Name: "aaa", Age: 24}

I implement the ModelBinder neaded for such a parameter (Person). I am calling this action from the client with the following instruction:

var person= {}; 
person.Name = "aaa"; person.Age = 24;
var **url = '/Person/Add/' + $.param(person)**;
**window.location = url;**

This is my first program in Asp.NET MVC. I thing this is the right way to write the 'url'. Could you please help me to create the variable 'url' (needed to call the server action) in the right format ?

Thinks

2
  • You mentioned ModelBinder in your question... Do you wish to add the Person Model, based on user input? Or am I'm not reading this correctly?\ Commented Jun 23, 2012 at 18:26
  • Actually I added this code in the server part. But I am using in the client part an URL which is badly interpreted by the server. It seems the way of writining the URL. If I have a complex JSON object with a vector, is there a way to transform it to an URL that suits the Asp.NET MVC server router? THINKS Commented Jun 23, 2012 at 21:08

1 Answer 1

1

You can pass that in the querystring like this

var thatUrl = "/Person/Add?Name=aaa&age=24";
thatUrl=encodeURI(thatUrl);  //Let's encode :)
window.location.href=thatUrl;

Assumuing you have HttpGET Action method which is looks like either

public ActionResult Add(string Name,string Age)
{
  //you will have the values in the argumens. Do something now
}

or

public ActionResult Add(Person model)
{
  //you will have the values in the object
  //check for model.Name & model.Age
}

assuming Name and Age are 2 properties of Person class

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

5 Comments

Is there a simple way to send a more complex object from the client objet to the server (more complex than the object Person = {Name: "aaa", Age: "24"}. Should I implement a javascript function to transform this object to the URL, or a function exsists yet to do this transformation ? Thinks
If you are making a jQuery ajax call, you can send the data as JSON
I would like to have a new URL adresse. So I would like to use some thing like window.location, not an ajax call. Thinks
I would like to have a new URL adresse. So I would like to use some thing like window.location, not an ajax call. In this case do you know please how to get a URL from a complex JSON object to use it with a window.location instruction? Or do you know how to do it in another way? I would like to move to a new URL (to be RESTfull). I don't think that using ajax will help me for that. Thinks
If it is rest, you can pass the items in your Content body ,If it is POST and pass it in parameters if it is GET. I think this one of yours is POST. so you can pass it in content body.

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.