knockoutjs with mvc collection model binding

淺唱寂寞╮ 提交于 2019-12-23 05:39:25

问题


I'm using knockoutjs to render a collection of items. After allowing the user to do some inline editing I need to post the collection back to the server. However, the collection isn't being populated on the server because I'm not using the name="[0].Blah" naming convention. Does anyone know how to either render name attributes like this using knockoutjs OR how to create a model binder that will allow me to extract the values from the ValueProvider?

You can see a screenshot of the ValueProvider during debugging below.

http://i.imgur.com/zSU5Z.png

Here is my managed ViewModel:

public class FundLevelInvestmentUploadResult
{
    public string FileName { get; set; }
    public IList<FundLevelInvestmentViewModel> Items { get; set; }
    public int NumberOfErrors { get; set; }

    public bool ShowErrorsOnly { get; set; }

    public FundLevelInvestmentUploadResult()
    {
        Items = new List<FundLevelInvestmentViewModel>();
    }
}

Here is the managed class for "Items":

public class FundLevelInvestmentViewModel
{
    private string _fund;
    private string _fundType;
    private string _date;
    private string _netOfWaivedFees;
    private string _waivedFees;
    private string _bcip;
    private string _fxRate;

    public uint RowIndex { get; set; }

    public int? DealCode { get; set; }
    public bool DealCodeIsValid { get; set; }

    public string Fund
    {
        get { return _fund; }
        set { _fund = GetString(value); }
    }
    public bool FundIsValid { get; set; }

    public string FundType
    {
        get { return _fundType; }
        set { _fundType = GetString(value); }
    }
    public bool FundTypeIsValid { get; set; }

    public string DateOfInvestment
    {
        get { return _date; }
        set { _date = GetString(value); }
    }
    public bool DateOfInvestmentIsValid { get; set; }

    public string NetOfWaivedFees
    {
        get { return _netOfWaivedFees; }
        set { _netOfWaivedFees = GetString(value); }
    }
    public bool NetOfWaivedFeesIsValid { get; set; }

    public string WaivedFee
    {
        get { return _waivedFees; }
        set { _waivedFees = GetString(value); }
    }
    public bool WaivedFeeIsValid { get; set; }

    public string BCIP
    {
        get { return _bcip; }
        set { _bcip = GetString(value); }
    }
    public bool BCIPIsValid { get; set; }

    public string ExchangeRateToUSD
    {
        get { return _fxRate; }
        set { _fxRate = GetString(value); }
    }
    public bool ExchangeRateToUSDIsValid { get; set; }

    public string FileName { get; set; }

    private IList<string> _errors;
    public IList<string> Errors
    {
        get { return _errors ?? (_errors = new List<string>());}
        set { _errors = value; }
    }

    public bool Show { get; set; }

    public FundLevelInvestmentViewModel()
    {
        Errors = new List<string>();
        Show = true;
    }

    // knockoutjs is returning "null" instead of "" for a null object when calling ko.mapping.fromJS
    private string GetString(string value)
    {
        if (value == "null")
            return string.Empty;

        return value;
    }
}

Here is my knockout viewModel:

var viewModel = {
    FileData: ko.observableArray([]),

    validateFile: function (file, event) {
        $.ajax({
            type: 'post',
            url: newUrl,
            data: ko.mapping.toJS(file)
        }).done(function (data) {
            var newFile = ko.mapping.fromJS(data);
            var index = file.Items.indexOf(file);
            viewModel.FileData.replace(file, newFile);
        });
    }
};

回答1:


If you are using version 2.1.0.0 or later of knockout you can render the name attribute as follows from an observable array.

<input data-bind='attr: { name: "Items["+$index()+"].DealCode"}' />


来源:https://stackoverflow.com/questions/11161910/knockoutjs-with-mvc-collection-model-binding

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