asp.net mvc - how to pass data from View to as an object Controller using ajax? -
i have search ajax request this:
$.ajax({ type: 'post', data: { firstname: firstname, lastname: lastname}, contenttype: "application/json; charset=utf-8", url: 'getpeople', datatype: 'json', } });
in getpeole action can parameter (firstname,lastname)
public virtual jsonresult getpeople(string firstname,string lastname) { .... }
if change ajax request
$.ajax({ type: 'post', data: { firstname: firstname, lastname: lastname,age=age}, contenttype: "application/json; charset=utf-8", url: 'getpeople', datatype: 'json', } });
i must change getpeople
public virtual jsonresult getpeople(string firstname,string lastname,int age) { .... }
i want searchparameters(firstname,lastname,age) object in getpeople
public virtual jsonresult getpeople(searchparam) { ..... }
you declare class parameter so:
public class searchfilters { public string firstname {get;set;} public string lastname {get;set;} public int age {get;set;} }
and use in controller this:
public jsonresult getpeople(searchfilters filters) { }
and in ajax post need pass data this:
data: json.stringify({ firstname: firstname, lastname: lastname,age=age})
Comments
Post a Comment