nam vo
nam vo

Reputation: 3437

Fill jquery array with server array?

on controller I have MODEL

 public partial class CategoryModel
{
...  
    public int[] SelectedCustomerIds { get; set; }
}

passed to a View from a controller. How can i fill the jquery array by the Model.SelectedCustomerIds

 <script type="text/javascript">
        var selectedIds = [];  << what to replace here

Upvotes: 0

Views: 384

Answers (3)

111
111

Reputation: 1779

If you want to use JavaScript side array pushing, then try this in view put all array elements in hidden @Html.Hidden("SelectedCustomerIds_"+i, Model.SelectedCustomerIds[i])
and in JavaScript

var selectedCustomerIds = [];
       $('*[id*=SelectedCustomerIds_]').each(function () {
        selectedCustomerIds.push($(this).val());
    });
 });

Upvotes: 0

Khurshid
Khurshid

Reputation: 944

You can use JSON.NET

<script type="text/javascript">
        var selectedIds = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.SelectedCustomerIds))

Upvotes: 1

Neel
Neel

Reputation: 11731

what you can do is u can first create object in jQuery and make a ajax call to server using AJAX and server side you will get tht object and you can simply transfer that object value to your model value!!

$.ajax({
                    type: "GET",        //GET or POST or PUT or DELETE verb
                    url: ajaxUrl,       // Location of the service
                    data: yourObject,       //Data sent to server
                    contentType: "",        // content type sent to server
                    dataType: "json",   //Expected data format from server
                    processdata: true,  //True or False
                    success: function (json) {//On Successful service call
                        var result = json.name;
                        $("#dvAjax").html(result);
                    },
                    error: ServiceFailed    // When Service call fails
                });

server side

using System.Web.Services;
    [WebMethod()]
    //[ScriptMethod()]
    public static void SendMessage(CategoryModel yourModelObject )
    {

    }

Upvotes: 1

Related Questions