Binding to ViewModel on PJax PostBack

ぃ、小莉子 提交于 2019-12-11 12:51:40

问题


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?

来源:https://stackoverflow.com/questions/35185255/binding-to-viewmodel-on-pjax-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!