我目前正在使用MVC4 SinglePage应用程序。使用参数通过Html.UpshotContext调用Web api方法
我有一个Web API方法GetChartsByCategory(INT CATID) 所以,在我看来CSHTML页怎能我在这种情况下宣布Html.UpshotContext。
我不想打电话给GetAllCharts(),然后使用敲出或结束在客户端过滤。
谢谢
我目前正在使用MVC4 SinglePage应用程序。使用参数通过Html.UpshotContext调用Web api方法
我有一个Web API方法GetChartsByCategory(INT CATID) 所以,在我看来CSHTML页怎能我在这种情况下宣布Html.UpshotContext。
我不想打电话给GetAllCharts(),然后使用敲出或结束在客户端过滤。
谢谢
使用Html.UpshotContext无法提供参数。您可以使用$ .ajax()调用GetChartsByCategory()并将结果映射到您的模型。
实施例:
$.ajax("GetChartsByCategory", true, {
data: { id: catID },
dataType: 'json',
success: function (data) {
// on success, data contains a list of charts
self.charts(ko.utils.arrayMap(data, function (item) {
return new Chart(item);
}));
}
});
型号:
Chart = function (initialData) {
var self = this;
// inject the initial data
$.each(initialData, function (key, value) {
self[key] = ko.observable(value);
});
....
}
另一个,允许你淘汰赛/结果框架内粘替代方法是改变结果提供参数操作名称为包括PARAMATERS作为部分的路由和/或查询字符串。
以下示例使用从HTML中收集的'ApplicationId'作为参数,将WebAPI调用作为路由('/ api/controller/action/id')的一部分接受'id'参数的方法:
控制器的方法:
public class ClientDetailsScreenController : DataController
{
public ClientModel GetClient(int id)
{
var client = //Fetch client with id using your prefered method
return client;
}
}
查看HTML结果语境:
@(Html.UpshotContext(true).DataSource<ClientDetailsScreenController>(ctr => ctr.GetClient(0))))
注意,传递给GetClient方法 '0' 得到由DataSourc忽略e方法可以是使方法签名有效的任何东西。
淘汰赛视图模型:
function ClientDetailsViewModel() {
var self = this;
//Store the operation name with a leading '/' for use later
self.operationNameTemplate = upshot.dataSources.Client._providerParameters.operationName + "/";
//Observable property for the ApplicationId enter via HTML
self.selectedApplicationId = ko.observable("1");
//Refresh Data Source Operation - Called in HTML when the ApplicaitonId changes
self.refresh = function() {
//Set the upshot operation name to include the id parameter value.
upshot.dataSources.Client._providerParameters.operationName = self.operationNameTemplate + self.selectedApplicationId();
//Refresh the data source causing the bound HTMl to be updated.
self.dataSource = upshot.dataSources.Client.refresh();
};
//Data Source Setup
self.refresh();
self.clients = self.dataSource.getEntities();
}
希望这有助于其他人谁一直试图获得通过结果传递给服务器的参数。如果有人有更好的方法,请让我们知道。