Monday, June 6, 2016

Pass Json Array/ List Any things to the server


Must Have HttpPost


Controller



 public class MyObjectPassController : Controller
    {
        // GET: Facility/MyObjectPass
        [HttpPost]
        public JsonResult Index(Request rq)
        {
            return Json(rq,JsonRequestBehavior.AllowGet);
        }
    }
    
    public class Request {

        public int RequestId { get; set; }
        public List<Person> Persons { get; set; }
        public List<Departments> Departments { get; set; }
        public List<string> OtherNames { get; set; }

    }
    public class Departments
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Person {

        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int DepId { get; set; }
    }

//////////////

Ts File


module PassObjectArray {
    $(document).ready(function () {

        var dep: Departments[] = [];
        var persons: Person[] = [];
        var otherNames: string[] = [];
        for (var index = 0; index < 5; index++) {

            dep.push({ Id: index, Name: "dep_" + index });
        }

        for (var index = 0; index < 5; index++) {
            persons.push({ Id: index, Name: "per_" + index, Age: index, DepId: 5 });
        }
        for (var index = 0; index < 10; index++) {
            otherNames.push("otherNames_" + index);
        }
        console.log(JSON.stringify({ RequestId: 1, Persons: persons, Departments: dep }));

        $.ajax({
            url: '/facility/MyObjectPass/Index',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            method: "POST",
            data: JSON.stringify({ RequestId: 1, Persons: persons, Departments: dep, OtherNames: otherNames }),
            success: (s) => {
                $('div').html(s);

            }
        });

    });
    class Departments {
        Id: number;
        Name: string;
    }

    class Person {

        Id: number;
        Name: string;
        Age: number;
        DepId: number;
    }

}

No comments:

Post a Comment

Sql server row level policy