0

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
2
  • Please avoid adding tags to the title. This is what the tags are for. Commented Oct 18, 2013 at 5:23
  • FYI, "jquery" is a framework, "javascript" is language. Your title is equivalent of asking "how to multiply two webforms numbers". Commented Oct 18, 2013 at 18:25

3 Answers 3

1

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

    }
Sign up to request clarification or add additional context in comments.

Comments

1

You can use JSON.NET

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.