knockoutjs与mvc集合模型绑定

时间:2012-06-22 18:15:20

标签: asp.net-mvc-3 knockout.js model-binding

我正在使用knockoutjs来渲染项目集合。在允许用户进行内联编辑之后,我需要将该集合发布回服务器。但是,没有在服务器上填充集合,因为我没有使用name =“[0] .Blah”命名约定。有谁知道如何使用knockoutjs渲染这样的名称属性或如何创建一个模型绑定器,允许我从ValueProvider中提取值?

您可以在下面的调试过程中看到ValueProvider的屏幕截图。

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

这是我管理的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>();
    }
}

以下是“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;
    }
}

这是我的淘汰视图模型:

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 个答案:

答案 0 :(得分:1)

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

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