通过jquery / ajax将复选框值传递给控制器​​,并将null值返回给控制器

时间:2017-04-06 16:27:14

标签: jquery asp.net-mvc

我有一个.NET MVC应用程序。我试图使用jquery ajax将数据发布到我的控制器。一切似乎都基于查看Firebug控制台(我可以在我的帖子请求中看到数组中的值),但在控制器中我为IEnumerable STRMS选择了null。关于我做错了什么想法?

enter image description here

部分视图

 @if (Model.Count() > 0)
    {

        foreach (var item in Model)
        {
            //int countAU = 1, countSP = 1, countSU = 1;
            string nameAU = "AU" + @item.year;
            string nameSP = "SP" + @item.year;
            string nameSU = "SU" + @item.year;
            <tr>
                <td style="text-align:center;">
                    <strong><label> @item.year</label></strong>
                </td>

                <td style="text-align:center;">


                    @if (@item.AU != null)
                    {
                        <input type="checkbox" name=@nameAU id="@nameAU" value="@item.AU" />
                        //countAU++;
                    }
                    else
                    {
                        <input type="checkbox" name="AUNonex" id="AUNone" disabled />
                    }

                </td>
           </tr>
          }
       }

主视图

 <div class="gradfacapp-samerow">
        <input type="submit" value="Save Request for Later" name="action:Save" class="cancel" />

        <input type="submit" value="Submit Request" id="submit" name="action:Submit" onclick="javascript:return checkSubmit();" />
 </div>


<script>
 $('#submit').on('click', function () {
        var STRMS = [];
        $('input:checked').each(function () {
            STRMS.push($(this).attr("value"));
        });
        $.ajax({
            type: "POST",
            url: '@Url.Action("Submit", "FeeAuth")',
            dataType: "html",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ STRMSelected: STRMS }),
            success: 
                function (data) {  }

        });
    });
</script>

控制器

    [HttpPost]
    [MultipleButton(Name = "action", Argument = "Submit")]
    public ActionResult Submit(FA fee, HttpPostedFileBase upfile, IEnumerable<string> STRMSelected)
    {              
          code is here
    }

3 个答案:

答案 0 :(得分:1)

删除contentType: "application/json; charset=utf-8",并更改

data: JSON.stringify({ STRMSelected: STRMS })data: { STRMSelected: STRMS},没有必要将数据作为json发送,因此您的ajax将

$.ajax({
            type: "POST",
            url: '@Url.Action("Submit", "FeeAuth")',
            dataType: "html",
            data: { STRMSelected: STRMS},
            success: 
                function (data) {  }

        });

修改

还会阻止默认提交

$('#submit').on('click', function (e) {
e.preventDefault(); 

答案 1 :(得分:0)

I notice that in the Ajax call you are setting data twice:

data: data,
data: JSON.stringify({ STRMSelected: STRMS }),

if that is not the issue, you could always wrap the inputs in a form then use

var formData = $("#myFormName").serializeArray();
var selectedItems = JSON.stringify(formData);

then in the Ajax call use:

data: selectedItems,

答案 2 :(得分:0)

你可以使用这个语法

 <input type="checkbox" name=@nameAU id="@nameAU" data-value="@item.AU" />

并进入jquery

var STRMS = [];
        $('input:checked').each(function () {
            STRMS.push($(this).data("value"));
        });

然后将STRMS发送到控制器。