mardi 4 août 2015

Pass Strongly Typed data and JSON string to controller from $.ajax funtion in View

I have ASP.NET-MVC5 application. I have strongly typed form and I successfully can pass back to controller. Now I have JavaScript array variable that I need to also send so I need to post back both information from view to controller using .$ajax post function.

I have update code as to add avaScript array variable, since then I am getting null value for form data.

View

var AdditionalTenentList = {
    StudentUWLID: []
};

 $('#CreateStudentRentingApplicationForm').submit(function (e) {

    e.preventDefault();

    var AdditionalTenentJsonList = JSON.stringify(AdditionalTenentList);

    alert(AdditionalTenentJsonList);

    var formURL = $(this).attr("action");


    $.ajax({
        url: formURL,
        type: "POST",
        data: { ApplicationModelData: $(this).serialize(), TenentJSONList: AdditionalTenentJsonList },
    }).done(function (data, textStatus, jqXHR) {
    //// my other code here.....
      }
</script>

In another function thats how I am pushing value to array

  AdditionalTenentList.StudentUWLID.push(StudentUWLID);

Controller

    [Authorize]
    [HttpPost]
    public ActionResult ApplyForAccommodation(AccommodationApplicationViewModel ApplicationModelData, string TenentJSONList)
    {

        return null;
    }

with the following code I get header response as

enter image description here

 $.ajax({
        url: formURL,
        type: "POST",
        dataType:"JSON",
        data: JSON.stringify({ TenentJSONList: AdditionalTenentList }),
    }).done(function (data, textStatus, jqXHR) {
   ..........

   public ActionResult ApplyForAccommodation(string [] TenentJSONList)
    {
       var a = "d";

        return null;
    }

Aucun commentaire:

Enregistrer un commentaire