use*_*163 7 javascript asp.net-mvc json asp.net-mvc-4
我试图从我的MVC控制器,抛出异常,序列化错误或使用JSON JavaScriptSerializer反序列化发送json.字符串的长度超过maxJsonLength属性上设置的值.
我用谷歌搜索并在我的配置中添加了最大长度,也覆盖了我的json方法,没有任何工作.
这是我的web配置和我的方法,它抛出异常.在appsetting
<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647">
</jsonSerialization>
</scripting>
</system.web.extensions>
Run Code Online (Sandbox Code Playgroud)
过度的方法
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonResult()
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior,
MaxJsonLength = Int32.MaxValue
};
}
Run Code Online (Sandbox Code Playgroud)
我的方法
public JsonResult GetAttributeControls()
{
List<SelectListItem> attrControls;
using (var context = new Context())
{
attrControls = context.AttributeControls.ToList().
Select(ac => new SelectListItem { Text = ac.Name, Value = ac.AttributeControlId.ToString() }).ToList();
}
//var jsonResult = Json(attrControls, JsonRequestBehavior.AllowGet);
//jsonResult.MaxJsonLength = int.MaxValue;
//return jsonResult;
return Json(attrControls,JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
我在下面的行中得到例外,这是我的load.chtml文件
<script type="text/javascript">
var InitialRowData = '@Html.Raw(Json.Encode(Model))';
var isLayoutEditable = false;
var occupied = '@Model.occupied';
var notoccupied = '@Model.notoccupied';
var blocked = '@Model.blocked';
</script>
Run Code Online (Sandbox Code Playgroud)
@ Html.Raw(Json.Encode(型号))';
有json的最大长度是20万左右,如何增加大小,没什么可锻炼的.有什么帮助吗?
提前致谢.
mho*_*ges 19
好的,所以我最近才有同样的问题.我试图做一个@Html.Raw(Json.Encode(Model))将模型发送到javascript并且收到字符串太长的错误.
我四处寻找答案,并找不到任何直接回答问题的答案,然而,我找到了这个堆栈答案,然后我用它来弄清楚如何解决我们的问题.
这里的想法是设置一个
JavaScriptSerializer,手动设置MaxJsonLength,序列化模型,然后将其传递给javascript.
@{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
var jsonModel = serializer.Serialize(Model);
}
Run Code Online (Sandbox Code Playgroud)
<script>
$(function () {
var viewModel = @Html.Raw(jsonModel);
// now you can access Model in JSON form from javascript
});
</script>
Run Code Online (Sandbox Code Playgroud)
小智 12
我最近遇到了同样的问题,并更改了我的序列化代码及其按预期工作。在您的代码中使用以下代码:
@using Newtonsoft.Json;
<div>@Html.Raw(JsonConvert.SerializeObject(Model))</div>
Run Code Online (Sandbox Code Playgroud)