Pass JSON Object To MVC Controller as an Argument

后端 未结 3 2148
时光取名叫无心
时光取名叫无心 2020-12-03 19:33

I have the following arbitrary JSON object(The field names may be changed).

  {
    firstname: \"Ted\",
    lastname: \"Smith\",
    age: 34,
    married : t         


        
3条回答
  •  离开以前
    2020-12-03 20:01

    Have a ViewModel with the same signature and use that as the argument type.Model binding will work then

    public class Customer
    {
      public string firstname { set;get;}
      public string lastname { set;get;}
      public int age{ set;get;} 
      public string location{ set;get;}
       //other relevant proeprties also
    }
    

    And your Action method will look like

    public JsonResult GetData(Customer customer)
    {
      //check customer object properties now.
    }
    

提交回复
热议问题