我当前的代码如下所示.如何将数组传递给控制器,控制器操作必须接受哪些参数?

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.

我的ASP.NET MVC视图代码如下所示:

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

我的控制器动作用一个自定义属性装饰

[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.反序列化对象来自Json.网

Link: Serializing and Deserializing JSON with Json.NET

Jquery相关问答推荐

Jquery如何通过数组中的属性查找对象

如何确定 jQuery 中匹配元素的元素类型?

如何从 jQuery 转到 React.js?

如何获取将在不提交的情况下提交的所有表单值

在文本框中的最后一个字符之后设置焦点

jQuery 1.4.1 中缺少 JSON 字符串化?

如何在 jquery 中包含 !important

在 jQuery 中构建 html 元素的最清晰方法

我如何从 ACE 编辑器中获得价值?

如何从 jQuery 中的父级中 Select 所有子级(任何级别)?

将输入更改为大写

如何使用 jQuery 进行带命名空间的 XML 解析

C# String.IsNullOrEmpty Javascript 等效项

使用 AJAX 加载 Bootstrap 弹出内容.这可能吗?

javascript,是否有像 isArray 这样的 isObject 函数?

Twitter Bootstrap alert 消息关闭并再次打开

将十六进制转换为 RGBA

如何使用 jquery 更改元素类型

如何使用 jQuery 或仅使用 Javascript 将按钮重定向到另一个页面

如何让 jQuery 等到效果完成?