-->

Binding to ViewModel on PJax PostBack

2019-08-15 01:54发布

问题:

I have a viewModel that I want to bind to pjax postback action:

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

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

This is the function I want to hit to handle the 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);
    }

My custom classes are generated using Entity Framework from DB:

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

My ASPX page look like this:

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

And I am trying to postback using this PJax code:

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

When I try this, MyViewModel only populate mySettings but not myDetails, it seem like that the list would not be binded, what am I doing wrong?