Che*_*hev 3 c# asp.net-mvc jquery json asp.net-mvc-3
我无法将JSON数组绑定到MVC 3中的C#列表.
我有一个叫做a的对象DockState
.它看起来像这样:
[Serializable]
public class DockState
{
public bool Closed { get; set; }
public bool Collapsed { get; set; }
public string DockZoneID { get; set; }
public int ExpandedHeight { get; set; }
public Unit Height { get; set; }
public int Index { get; set; }
public Unit Left { get; set; }
public bool Pinned { get; set; }
public bool Resizable { get; set; }
public string Tag { get; set; }
public string Text { get; set; }
public string Title { get; set; }
public Unit Top { get; set; }
public string UniqueName { get; set; }
public Unit Width { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的action方法接受停靠状态列表,如下所示:
public JsonResult SaveDockStates(List<DockState> dockStates)
Run Code Online (Sandbox Code Playgroud)
在客户端上,我正在构建一个JSON对象数组,其中包含与C#DockState
对象相同的所有属性.然后我将此数组赋值给它自己的JSON对象,并使用action方法中的参数的属性,如下所示:
function get_allDockStates()
{
var allRadDocks = []; // declare array.
var allRadControls = $telerik.radControls; // use telerik API to obtain all controls.
// loop through all telerik controls.
for (var i = 0; i < allRadControls.length; i++)
{
var element = allRadControls[i];
// Check if control is a rad dock element.
if (Telerik.Web.UI.RadDock && element instanceof Telerik.Web.UI.RadDock)
{
// Build a JSON object containing the same properties as the C# DockState
// object. Leaving out a couple that should just be null anyway. Add new
// JSON object to array.
Array.add(allRadDocks,
{
UniqueName: element._uniqueName,
DockZoneID: element._dockZoneID,
Width: element._width,
Height: element._height,
ExpandedHeight: element._expandedHeight,
Top: element._top,
Left: element._left,
Resizable: element._resizable,
Closed: element._closed,
Collapsed: element._collapsed,
Pinned: element._pinned,
Title: element._title,
Index: element._index
});
}
}
// Return the array.
return allRadDocks;
}
// This function is fired by the rad dock controls when they are moved.
function positionChanged(sender, e)
{
// obtain the array of dock states.
var dockStates = get_allDockStates();
// Make ajax call to MVC action method to save dock states.
jQuery.ajax({
data: { dockStates: dockStates },
type: 'POST',
dataType: 'json',
url: '/AjaxServices/DashboardService/SaveDockStates'
});
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,在进行AJAX调用时会发生一些奇怪的事情.它命中了动作方法,我List<DockState>
的确有物品.
http://www.codetunnel.com/content/images/dockStateListInstantiated.jpg
但是,列表中的项目都是默认值.意味着它们的所有值都是默认值,而不是请求中提交的值.
http://www.codetunnel.com/content/images/dockStatesDefaultValues.jpg
我不确定为什么列表包含正确数量的项目,但所有项目似乎只是实例化.它们的属性未设置为JSON数组中的值.请求肯定包含正确的数据,如下所示:
http://www.codetunnel.com/content/images/dockStatesFormData.jpg
难道我做错了什么?表单数据格式不正确吗?默认模型绑定器有问题吗?
任何帮助深表感谢.
我发现JSON被一个未使用的旧脚本覆盖了.我已删除,现在JSON.stringify工作正常.这是请求有效负载.
http://www.codetunnel.com/content/images/dockStatesJsonStringify.jpg
好多了.但是现在当我调试时,我的列表中没有任何项目.似乎没有解决问题,虽然我很感激努力:)
Dar*_*rov 14
尝试发送JSON请求:
jQuery.ajax({
// TODO: Never hardcode urls like this => always use URL helpers
// when you want to generate an url in an ASP.NET MVC application
url: '/AjaxServices/DashboardService/SaveDockStates',
type: 'POST',
data: JSON.stringify({ dockStates: dockStates }),
contentType: 'application/json; charset=utf-8',
success: function(result) {
// TODO: process the results
}
});
Run Code Online (Sandbox Code Playgroud)
该JSON.stringify
方法原生于现代浏览器中.如果要支持旧版浏览器,则需要在页面中包含json2.js脚本.
归档时间: |
|
查看次数: |
7644 次 |
最近记录: |