繁体   English   中英

将大型 Model 列表从视图发送到 controller c# mvc Z60C05C632A28229A8737C72E

[英]Sending large Model list from view to controller c# mvc razor

我有非常大的 model 列表,我想使用 ajax 查询将列表发送回 controller。 我试图将整个 model 列表发回,但由于 model 太大,它超过了 Z71867A5EC93E05EB 中指定的 json 最大长度。 编码方法虽然适用于较小的列表。

var jsonString = @Html.Raw(Json.Encode(Model.modelName_small));

我可以想象它工作的唯一方法是使用 javascript 将大型 model 列表过滤为较小的列表(类似于“Where” SQL 语句)。 我的脚本如下(剃刀):

<script type="text/javascript" language="javascript">

    function functionName(input1_decimal) {

        var smallerList = new Array();

        @foreach (var item in Model.modelName)
        {
            //input1_decimal should be within a certain range
            @:if (input1_decimal - 0.1 <= @item.Property1) && (@item.Property1 <= input1_decimal + 0.1)
            {
                @:smallerList.push("@item");
            }            
        }
        //convert smallerList to json and send it to controller
     }

<script>

这似乎很简单,但我就是无法让它工作。 可能是一件非常微不足道的事情。 我也试过:

var smallerList= Model.modelName.Where(x => (input1_decimal - 0.1 <= x.Property1) && (x.Property1 <= input1_decimal + 0.1));

同样,我也尝试过

        var smallerList = Model.modelName.filter(function (item) {
           return (input1_decimal - 0.1 <= item.Property1) && (item.Property1<= input1_decimal + 0.1)
        }); 

感谢您的耐心等待。 我希望我已经清楚地解释了我想要实现的目标。 我不是开发人员。 编程只是为了好玩和自学。

您是否正在修改视图上的数据? If so, one other approach is to post only modified data to the controller in order to minimized the json string length and retrieve the rest of the data directly in the controller.

我没有在 web.config 中编辑 jsonmaxlength 字段,而是将 MaxJsonLength 分配给 Int32.MaxValue。 创建了一个列表并将属性分配给 model 属性并序列化为 Json object 列表。 然后我使用 $.grep function 过滤列表。 最后,我能够将 objJsonSmallList 发送回 controller ......快乐的日子:)

@{
     var js = new System.Web.Script.Serialization.JavaScriptSerializer();
     js.MaxJsonLength = Int32.MaxValue;
     //Create a list and assigning all the properties of the model
     var data = Model.model_Name.Select(x => new
            {
                propName1 = x.property1,
                propName2 = x.property2,
                ...
                propNameN = x.propertyN
             });
      //serialize collection of anonymous objects
      string strArr = js.Serialize(data);
}

var objJsonBigList = JSON.parse('@strArr'.replace(/&quot;/g, '"'));

//small Filtered list send to controller via Ajax
var objJsonSmallList = $.grep(objJsonBigList, function (n) {
   return ((input1_decimal- 0.1 <= n.Prop) && (n.Prop <= input1_decimal + 0.1))
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM