Send javascript array to Ajax.BeginForm

I am working on a project and I received a request to send a javaScript array "selectedZonesList", returning the data back to the controller along with the form data. I was asked to use Ajax.BeginForm ... but I'm trying my best to put all the details ...

in partial view

@using (Html.BeginForm("CreateNewFeeScheme", "Qualification", FormMethod.Post, new { id = "NewFeeSchemeForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

   //rest of code to take user input for all variables ..

<input type="submit" value="Create" class="btn btn-default" />
}

Javascript function

 <script type="text/javascript">

 var selectedZonesList = new Array();

 function AddFeeZoneToScheme(e)
 {

    var entityGrid = $("#FeeZoneGrid_02").data("kendoGrid");

    var selectedZone = entityGrid.dataItem(entityGrid.select());

    selectedZone = selectedZone.FeeZoneID;

    selectedZonesList.push(selectedZone); 
}

</script>

controller

  [HttpPost]
    public ActionResult CreateNewFeeScheme(FeeScheme newSchemeData, ??????)
    {
+4
source share
3 answers

I have a problem sending data together, but thanks to the guidance of ramiramilu, I figured out a solution ....

Get the SerializeObject Plugin

$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
   if (o[this.name]) {
       if (!o[this.name].push) {
           o[this.name] = [o[this.name]];
       }
       o[this.name].push(this.value || '');
   } else {
       o[this.name] = this.value || '';
   }
});
return o;
};

script

    function submit_createNewFeeScheme()
{

    $.ajax({
        type: "Post",
        url: "/Qualification/CreateNewFeeScheme",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ ZonesList: selectedZonesList, newFeeSchemeData:  $("#NewFeeSchemeForm").serializeObject() }),
        success: function (data) {
            alert(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
}
0
source

JQuery AJAX POST. Stirngly Typed AJAX JQuery.

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    function submitForm() {
        var roles = ["role1", "role2", "role3"];

        var model = new Object();
        model.Name = "Rami";

        jQuery.ajax({
            type: "POST",
            url: "@Url.Action("AddUser")",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ Roles: roles, person: model }),
            success: function (data) { alert(data); },
            failure: function (errMsg) {
                alert(errMsg);
            }
        });
    }
</script>

<input type="button" value="Click" onclick="submitForm()"/>

-

    public ActionResult AddUser(string[] Roles, PersonViewModel person)
    {
        return null;
    }

, :

enter image description here

, , - PersonViewModel -

public class PersonViewModel
{
    public string Name { get; set; }
}
+3

javascript .

  • , , , .

  • Ajax, .

SO

JS Array mvc ajax

0

Source: https://habr.com/ru/post/1525591/


All Articles