问题描述
我目前的code看起来像下面这样。我该如何通过我的阵列控制器,必须我控制器操作接受什么样的参数?
函数getplaceholders(){
VAR占位符= $('。UI可排序');
VAR的结果=新的Array();
placeholders.each(函数(){
VAR的pH = $(本).attr('身份证');
VAR部分= $(本).find('排序');
VAR部分; sections.each(函数(一,项目){
VAR SID = $(项目).attr('身份证'); result.push({'SectionId':SID,占位符:pH值,'位置':我});
});
});
警报(result.toString());
$。员额(
/portal/Designer.mvc/SaveOrUpdate',
结果,
功能(数据){
警报(data.Result);
}JSON);
};
我的控制器的操作方法看起来像
公共JsonResult SaveOrUpdate(IList的< PageDesignWidget>小部件)
我已经找到了解决方案。我用史蒂夫詹蒂莱的解决方案,的 的
我的ASP.NET MVC视图code如下:
函数getplaceholders(){
VAR占位符= $('。UI可排序');
VAR的结果=新的Array();
placeholders.each(函数(){
VAR的pH = $(本).attr('身份证');
VAR部分= $(本).find('排序');
VAR部分; sections.each(函数(一,项目){
VAR SID = $(项目).attr('身份证');
变种O = {'SectionId':SID,占位符:pH值,'位置':I};
results.push(O);
});
});
VAR POSTDATA = {小部件:结果};
VAR部件=结果;
$阿贾克斯({
网址:'/portal/Designer.mvc/SaveOrUpdate',
输入:POST,
数据类型:JSON,
数据:$ .toJSON(小部件),
的contentType:应用/ JSON的;字符集= UTF-8,
成功:函数(结果){
警报(result.Result);
}
});
};
和我控制器操作饰有一个自定义属性。
[JsonFilter(参数=小工具,JsonDataType = typeof运算(名单< PageDesignWidget>))]
公共JsonResult SaveOrUpdate(列表< PageDesignWidget>小部件
code自定义属性可以在这里(即现在的链路断开)发现
My current code looks like the following. How can I pass my array to the controller and what kind of parameters must my controller action accept?
function getplaceholders() {
var placeholders = $('.ui-sortable');
var result = new Array();
placeholders.each(function() {
var ph = $(this).attr('id');
var sections = $(this).find('.sort');
var section;
sections.each(function(i, item) {
var sid = $(item).attr('id');
result.push({ 'SectionId': sid, 'Placeholder': ph, 'Position': i });
});
});
alert(result.toString());
$.post(
'/portal/Designer.mvc/SaveOrUpdate',
result,
function(data) {
alert(data.Result);
}, "json");
};
My controller action method looks like
public JsonResult SaveOrUpdate(IList<PageDesignWidget> widgets)
I've found an solution. I use an solution of Steve Gentile, jQuery and ASP.NET MVC – sending JSON to an Action – Revisited.
My ASP.NET MVC view code looks like:
function getplaceholders() {
var placeholders = $('.ui-sortable');
var results = new Array();
placeholders.each(function() {
var ph = $(this).attr('id');
var sections = $(this).find('.sort');
var section;
sections.each(function(i, item) {
var sid = $(item).attr('id');
var o = { 'SectionId': sid, 'Placeholder': ph, 'Position': i };
results.push(o);
});
});
var postData = { widgets: results };
var widgets = results;
$.ajax({
url: '/portal/Designer.mvc/SaveOrUpdate',
type: 'POST',
dataType: 'json',
data: $.toJSON(widgets),
contentType: 'application/json; charset=utf-8',
success: function(result) {
alert(result.Result);
}
});
};
and my controller action is decorated with an custom attribute
[JsonFilter(Param = "widgets", JsonDataType = typeof(List<PageDesignWidget>))]
public JsonResult SaveOrUpdate(List<PageDesignWidget> widgets
Code for the custom attribute can be found here (the link is broken now).
Because the link is broken this is the code for the JsonFilterAttribute
public class JsonFilter : ActionFilterAttribute
{
public string Param { get; set; }
public Type JsonDataType { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
{
string inputContent;
using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
{
inputContent = sr.ReadToEnd();
}
var result = JsonConvert.DeserializeObject(inputContent, JsonDataType);
filterContext.ActionParameters[Param] = result;
}
}
}
JsonConvert.DeserializeObject is from Json.NET
Link: Serializing and Deserializing JSON with Json.NET
这篇关于如何发布使用JSON,jQuery的复杂对象的数组ASP.NET MVC控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!