Domanda

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

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top