0

I have a search ajax request like this:

  $.ajax({
        type: 'POST',
        data: { FirstName: firstname, LastName: lastname},
        contentType: "application/json; charset=utf-8",
        url: 'GetPeople',
        dataType: 'json',

      }
    });

In GetPeole action I can get my parameter (FirstName,LastName)

public virtual JsonResult GetPeople(string FirstName,string LastName)
        {
          ....
        }

If i change my ajax request like

$.ajax({
        type: 'POST',
        data: { FirstName: firstname, LastName: lastname,Age=age},
        contentType: "application/json; charset=utf-8",
        url: 'GetPeople',
        dataType: 'json',

      }
    });

I must change my GetPeople

 public virtual JsonResult GetPeople(string FirstName,string LastName,int Age)
        {
          ....
        }

I want get my searchparameters(FirstName,LastName,Age) as an object in Getpeople like this

public virtual JsonResult GetPeople(searchParam)
    {
        .....
    }

1 Answer 1

2

You declare a class for your parameter like so:

public class SearchFilters {
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public int Age {get;set;}
}

and use it in your controller like this:

public JsonResult GetPeople(SearchFilters filters) {
}

and in your ajax post you need to pass the data like this:

data: JSON.stringify({ FirstName: firstname, LastName: lastname,Age=age})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.