【发布时间】:2012-06-25 07:53:18
【问题描述】:
我正在使用 knockoutjs 来呈现项目集合。在允许用户进行一些内联编辑后,我需要将集合发布回服务器。但是,该集合没有在服务器上填充,因为我没有使用 name="[0].Blah" 命名约定。有谁知道如何使用 knockoutjs 呈现这样的名称属性,或者如何创建一个模型绑定器以允许我从 ValueProvider 中提取值?
下面可以看到ValueProvider在调试过程中的截图。
这是我的托管 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);
});
}
};
【问题讨论】:
-
如果您使用
toJSON发送回服务器,它应该可以工作。你能发布你的模型吗? -
你试过发帖
file.FileData吗? -
我不想发布所有的 FileData。只是 FileData 的一个单独元素,它是一个完整的 FundLevelInvestmentUploadResult 实例。
-
啊,我对发布的收藏感到困惑。那么,该对象是填充了一个空的
Items集合,还是整个都是空的? -
奇怪的是,集合中有正确数量的项目,但每个项目的所有属性都是默认值(字符串为 null,int 为 0,等等)。
标签: asp.net-mvc-3 knockout.js model-binding