0
我的頁面上有一些Knockout.js代碼,根據下拉列表的選定值生成可下載文件的列表 - 下載的來源位於SQL數據庫中,我只是存儲物理文件名 - 列表通過ASP.Net MVC控制器操作返回到Json格式的頁面。相關淘汰賽代碼:Knockout.js URL編碼
$(document).ready(function() {
function DDLViewModel() {
var self = this;
self.ddlDim1 = ko.observableArray([]);
self.selectedDim1 = ko.observable();
self.selectedDim1.subscribe(FetchVariant);
//default values from Model
self.PDFs = ko.observableArray(@Html.Raw(Json.Encode(Model.PDFs)));
function FetchVariant() {
if (self.selectedDim1())
{
$.ajax({
type: 'POST',
url: '@Url.Action("GetVariant")', // we are calling json method
dataType: 'json',
// here we get value of selected dim.
data: { productId: @Model.Product.ProductID,
Dim1: self.selectedDim1()
},
success: function (variant) {
// variant contains the JSON formatted variant data passed from the controller
var newpv = [];
newpv.push(variant);
self.PDFs(newpv[0].PDFs);
},
error: function (ex) {
//alert('Failed to retrieve variant.' + ex);
self.PDFs(@Html.Raw(Json.Encode(Model.PDFs)));
}
});
}
else
{
//not selected so reset all values back to Model
self.PDFs(@Html.Raw(Json.Encode(Model.PDFs)));
}
}
}
ko.applyBindings(new DDLViewModel);
})
<div data-bind="visible: PDFs().length">
<h3>Downloads</h3>
<!-- ko foreach: PDFs -->
<p><a data-bind="attr: { href: '/artwork/ProdPDF/' + FileName }, text: Title" target="_blank"></a></p>
<!-- /ko -->
</div>
我的問題是,文件名沒有被編碼,所以任何文件名以說,在一個&符不工作。有沒有一種方法可以在Knockout代碼中內聯編碼url/filename?