将强类型数据和JSON字符串从View中的$ .ajax函数传递给控制器

时间:2015-08-03 13:49:56

标签: c# jquery json asp.net-mvc-5 jquery-ajaxq

我有ASP.NET-MVC5应用程序。我有强烈的打字形式,我成功地可以传回控制器。现在我有需要发送的JavaScript数组变量,所以我需要使用。$ ajax post函数将两个信息从视图回发到控制器。

我有更新代码以添加avaScript数组变量,从那时起我获得了表单数据的空值。

视图

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>

在另一个函数中,我将如何将值推送到数组

  AdditionalTenentList.StudentUWLID.push(StudentUWLID);

控制器

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

        return null;
    }

使用以下代码我得到标题响应为

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;
    }

1 个答案:

答案 0 :(得分:0)

我找到的答案如下;

 var AdditionalTenentList = new Array();

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

    e.preventDefault();

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

    var formData = $(this).serialize();


    $.ajax({
        url: formURL,
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ TenentJSONList: AdditionalTenentList, ApplicationModelData: $("#CreateStudentRentingApplicationForm").serializeObject() }),
    }).done(function (data, textStatus, jqXHR) {

      //  .........my rest of code

...

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