与MVC收集模型绑定knockoutjs(knockoutjs with mvc collectio

2019-09-18 09:45发布

我使用knockoutjs渲染项目的集合。 允许用户做一些行内编辑后,我需要发布收集回服务器。 然而,集合不会在服务器上,因为我不使用名称=“[0] .Blah”命名约定填充。 有谁知道如何可以呈现name属性像这样使用knockoutjs或如何创建一个模型绑定,让我从中提取ValueProvider值是多少?

你可以看到下面调试期间ValueProvider的屏幕截图。

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

这里是我的管理视图模型:

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

下面是“项目”的管理类:

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

这里是我的淘汰赛视图模型:

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

Answer 1:

如果您使用的版本2.1.0.0或更高版本的淘汰赛,你可以渲染的name属性从可观察到的数组如下。

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


文章来源: knockoutjs with mvc collection model binding