0

在控制器上我有模型

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

从控制器传递给视图。如何通过 Model.SelectedCustomerIds 填充 jquery 数组

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

3 回答 3

1

您可以使用JSON.NET

<script type="text/javascript">
        var selectedIds = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.SelectedCustomerIds))
于 2013-10-18T05:35:51.350 回答
1

您可以做的是您可以首先在 jQuery 中创建对象并使用 AJAX 和服务器端对服务器进行 ajax 调用,您将获得该对象,您可以简单地将该对象值转移到您的模型值!

$.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
                });

服务器端

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

    }
于 2013-10-18T05:29:20.893 回答
0

如果您想使用 JavaScript 端数组推送,请尝试在视图中将所有数组元素隐藏 @Html.Hidden("SelectedCustomerIds_"+i, Model.SelectedCustomerIds[i])
并在 JavaScript 中

var selectedCustomerIds = [];
       $('*[id*=SelectedCustomerIds_]').each(function () {
        selectedCustomerIds.push($(this).val());
    });
 });
于 2013-10-18T05:58:34.270 回答