0

I have a .NET MVC application. I am attempting to post data to my controller using jquery ajax. Everything seems to be working based on viewing the Firebug console (I can see the values in an array in my post request) but in the controller I am getting null for IEnumerable STRMSelected. Any ideas on what I am doing wrong??

enter image description here

partial view

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

Main View

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

Controller

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

3 Answers3

1

remove contentType: "application/json; charset=utf-8", and change

data: JSON.stringify({ STRMSelected: STRMS }), to data: { STRMSelected: STRMS}, it is not necessary to send the data as json so your ajax will be

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

        });

Edit

also prevent the default submit

$('#submit').on('click', function (e) {
e.preventDefault(); 
Usman
  • 4,615
  • 2
  • 17
  • 33
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,
Simon
  • 764
  • 4
  • 8
  • I removed the data: data but it still displays as null in the controller. I added a screenshot of the post from Firebug. – user2448412 Apr 06 '17 at 17:02
  • Try changing the STRMSelected input to a string, then deserialize it within the controller to get the particular values you want – Simon Apr 06 '17 at 17:19
0

you can use this syntax

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

and getting in jquery

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

Then send STRMS to controller.

hamed hossani
  • 986
  • 2
  • 14
  • 33