绑定到PJax PostBack上的ViewModel

时间:2016-02-03 18:50:44

标签: c# asp.net-mvc entity-framework pjax

我有一个viewModel,我想绑定到pjax回发操作:

public class MyViewModel
{
    public MySetting mySetting{ get; set; }

    public IList<MyDetail> myDetails{ get; set; }
}

这是我想要处理postBack

的功能
    [HttpPost]
    public ActionResult SaveMySettings(MyViewModel viewModel)
    {
        viewModel.mySettings;  // This populate fine
        viewModel.myDetails;   // Is NULL
        // handle saving here
        return this.PAjax("myPage", null, viewModel);
    }

我的自定义类是使用DB中的Entity Framework生成的:

public class MySetting
{
    private bool _settingA;             
    [ColumnAttribute(Storage="_settingA", DbType="Bit NOT NULL")]
    public bool settingA
    {
        get
        {
            return this._settingA;
        }
        set
        {
            if ((this._settingA!= value))
            {
                this.OnsettingAChanging(value);
                this.SendPropertyChanging();
                this._settingA= value;
                this.SendPropertyChanged("settingA");
                this.On_settingAChanged();
            }
        }
    }
}   
public class MyDetail
{
    private bool _detailA;             
    [ColumnAttribute(Storage="_detailA", DbType="Bit NOT NULL")]
    public bool detailA
    {
        get
        {
            return this._detailA;
        }
        set
        {
            if ((this._detailA!= value))
            {
                this.OnsettingAChanging(value);
                this.SendPropertyChanging();
                this._detailA= value;
                this.SendPropertyChanged("detailA");
                this.On_detailAChanged();
            }
        }
    }
}       

我的ASPX页面如下所示:

@model MyProject.MyViewModel
<div id="main">
<form id="myForm" post="/Settings/SaveMySettings">
    @Html.TextBoxFor(m => m.mySetting.settingA)
    @for (int i = 0; i < Model.myDetails.Count(); i++){
        @Html.CheckBoxFor(m => Model.myDetails[i].detailA, @checked = "checked" }) MyCheckBox @Model.myDetails[i].detailA
    }
    <a href="#" id="saveChanges">Save Settings</a>
</form>
</div>

我正在尝试使用此PJax代码进行回发:

<script lang="javascript">
    (function ($) {
        $('#saveChanges').click(function () {

            var f = $(this).closest('form');
            $.pjax({
                type: 'POST',
                url: "/Settings/SaveMySettings",
                container: '#main',
                data: $("#myForm").serializeArray()
            });
            return false;
        });
    })(jQuery);
</script>

当我尝试这个时,MyViewModel只填充mySettings但不填充myDetails,看起来列表不会被绑定,我做错了什么?

0 个答案:

没有答案