简体   繁体   English

如何发布一个复杂的模型来行动?

[英]How to post a complex model to action?

My model defined: 我的模型定义为:

public class RulePageViewModel
{
    public List<RuleItem> RuleItemList { get; set; }

    public RuleViewModel RuleViewModel { get; set; }
}

My action defined: 我的动作定义为:

public JsonResult Save(RulePageViewModel viewmodel)

I try to post json, viewmodel.RuleItemList.Count > 0, but instance in viewmodel.RuleItemList is null. 我尝试发布json,viewmodel.RuleItemList.Count> 0,但viewmodel.RuleItemList中的实例为null。 If use model bind,how to bind a list in view? 如果使用模型绑定,如何在视图中绑定列表?

I haven't try to bind model, just use ajax to post json to action. 我没有尝试绑定模型,只需使用ajax将json发布即可。 I think it will work, but failed Code: 我认为它将起作用,但是代码失败:

var s = { "RuleItemList": [{ "RuleGroupId": 1, "RuleGroupName": "", "Keywords": "ajax", "ResponseDescribe": "dadhsa" }], "RuleViewModel": { "RuleGroupId": 14, "RuleList": [{ "RuleId": 567, "SourceId": 125, "KeyValue": "callback", "SourceType": 0 }], "SourceList": [] } };
var ss = JSON.stringify(s);
var json = JSON.parse(ss);
$.ajax({
    url: '@Url.Action("Save")',
    type: 'POST',
    data: json,
    dataType: 'json',
    success: function(response) {
        alert('success');
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert(textStatus);
    }
});

OK, I solved it: 好的,我解决了:

var json = { "RuleItemList": [{ "RuleGroupId": 1, "RuleGroupName": "", "Keywords": "ajax", "ResponseDescribe": "dadhsa" }], "RuleViewModel": { "RuleGroupId": 14, "RuleList": [{ "RuleId": 567, "SourceId": 125, "KeyValue": "callback", "SourceType": 0 }], "SourceList": [] } };

$.ajax({
    url: '@Url.Action("Save")',
    type: 'POST',
    data: JSON.stringify(json),
    dataType: 'json',
    contentType: 'application/json',
    success: function(response) {
        alert('success');
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert(textStatus);
    }
});

Thanks all! 谢谢大家!

Generally you should do something like that: 通常,您应该执行以下操作:

    @for(var i = 0;i<Model.RuleItemList.Count;++i)
    {
        @Html.TextBoxFor(m => m.RuleItemList[i].Name);
    }

    @Html.EditorFor(m = > m.RuleViewModel.PropertyOne);
    @Html.EditorFor(m = > m.RuleViewModel.PropertyTow);
    @Html.EditorFor(m = > m.RuleViewModel.PropertyThree);

Which will eventually generate something like that to the html: 最终将为html生成类似的内容:

<input type="text" name="RuleViewModel.PropertyOne" value="" />
<input type="text" name="RuleViewModel.PropertyTow" value="" />
<input type="text" name="RuleViewModel.PropertyThree" value="" />

Now because you did not place any view code or any code from your RuleItem an RuleViewModel classes that is going to be some problem to specifically answer your question so please post more info but in general you should understand from what i wrote here how to implement such a from that will post the data to your controller action so that the Model Binder will know which properties to bind.. 现在,因为您没有放置任何视图代码或RuleItem的任何代码, RuleItem RuleViewModel类将特别需要回答您的问题,因此请发布更多信息,但总的来说,您应该从我在这里写的内容中了解如何实现此类将从中将数据发布到您的控制器操作中,以便Model Binder知道要绑定的属性。

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM