MVC - 如何ModelBind名单与这种不寻常的POST数据格式的对象吗? [英] MVC - How to ModelBind a list an objects with this unusual post data format?

查看:287
本文介绍了MVC - 如何ModelBind名单与这种不寻常的POST数据格式的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当您使用ModelBind MVC对象的列表,动作看起来就像这样:

 公众的ActionResult你好(列表< D​​ocumentViewModel>的ViewModels)
    {

和邮政数据看起来是这样的:

  [0] .ID 1
[0] .Name点鲍伯
[1] .ID 2
[1] .Name点简

但是,我怎么能得到以下后的数据?

  0 [ID] 1
0名]鲍勃
1 [ID] 2
1 [名称]简

POST数据是由剑道UI提供的,因此我无法想象我有更大的灵活性去改变它。

更新感谢Petur答案。为了扩大他的回答,这个功能:

  common.MvcSerializeArray =功能(数组,preFIX){
    VAR的结果= {};    如果(!preFIX){
        preFIX ='';
    }    如果(阵列){
        对于(VAR I = 0; I< array.length,我++){
            如果($ .isPlainObject(数组[我])){
                对于(数组VAR财产[I]){
                    结果[preFIX +[+ I +。 +属性] =阵列[我] [属性]
                }
            }其他{
                结果[preFIX +[+ I +]] =阵列[我]
            }
        }
    }    返回结果;
};

在返回剑道的数据的方法之前,应该换阵:

  .DataSource(数据源=>数据源
        阿贾克斯()
        .Read(读=>读
            .Action(PickerDocuments,的DocumentRepository)
            。数据(@<文字和GT;功能(E){返回MyApp.Common.MvcSerializeArray(@ Html.Raw(Json.En code(型号)));}< /文本>)))

和瞧,我得到的数据后MVC ModelBinds轻松:

  [0] .ID 1
[0] .Name点鲍伯
[1] .ID 2
[1] .Name点简


解决方案

您不能改变它,但你可以通过发送addiotional参数,如解释添加所需的格式<一个href=\"http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/faq#how-do-i-send-values-to-my-action-method-when-binding-the-grid?\"相对=nofollow>这里阿贾克斯电网。同样的做法 code库(你可以得到code里面的神奇功能,它被用来发送多选值)

Normally when you ModelBind a list of objects with MVC, the action looks like so:

    public ActionResult Hello(List<DocumentViewModel> viewModels)
    {

And the Post data looks something like:

[0].Id 1
[0].Name Bob
[1].Id 2
[1].Name Jane

But, how could I get the following post data to work?

0[Id] 1
0[Name] Bob
1[Id] 2
1[Name] Jane

The Post data is being provided by Kendo UI, so I don't imagine I have much flexibility to change it.

Update Thanks Petur for the answer. To expand on his answer, this function:

common.MvcSerializeArray = function (array, prefix) {
    var result = {};

    if (!prefix) {
        prefix = '';
    }

    if (array) {
        for (var i = 0; i < array.length; i++) {
            if ($.isPlainObject(array[i])) {
                for (var property in array[i]) {
                    result[prefix + "[" + i + "]." + property] = array[i][property];
                }
            } else {
                result[prefix + "[" + i + "]"] = array[i];
            }
        }
    }

    return result;
};

should wrap arrays before being returned to Kendo's Data method:

    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read
            .Action("PickerDocuments", "DocumentRepository")
            .Data(@<text>function(e) { return MyApp.Common.MvcSerializeArray(@Html.Raw(Json.Encode(Model))); }</text>)))

And voila, I get post data MVC ModelBinds with ease:

[0].Id 1
[0].Name Bob
[1].Id 2
[1].Name Jane

解决方案

You cannot change it but you can add the desired format by sending addiotional parameters like explained here for the Ajax Grid. Same approach is used in this code library (you can get the magic function inside of the code, it is used to send the multi-select values)

这篇关于MVC - 如何ModelBind名单与这种不寻常的POST数据格式的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆