简体   繁体   中英

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. 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. 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:

<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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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