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

minchiya
  • 603
  • 1
  • 7
  • 13
  • 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?\ – jacqijvv Jun 23 '12 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 – minchiya Jun 23 '12 at 21:08

1 Answers1

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

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 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 – minchiya Jun 23 '12 at 21:57
  • If you are making a jQuery ajax call, you can send the data as JSON – Shyju Jun 23 '12 at 22:07
  • 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 – minchiya Jun 23 '12 at 22:33
  • 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 – minchiya Jun 23 '12 at 22:44
  • 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. – Shyju Jun 24 '12 at 00:25