我正在使用 Razor 页面,但无法使用 jquery .load 函数将 javascript 中的 dto 对象映射到模型中的类。
因此,用户单击 UI 中的按钮并运行以下 javascript:
$('#btnGoToResults').click(function (e) {
var dto = {
ID: 1,
CODE: 5
};
$('#divPerformanceResults').load('/PerformanceSearch?handler=ResultsPartial', dto); // Gives error 400
}
我也尝试了以下方法而没有让它工作:
$('#divPerformanceResults').load('/PerformanceSearch?handler=ResultsPartial', JSON.stringify(dto)); // "works" since the code behind is hit but the dto values are 0
还尝试用ajax重写:
// Gives error 400
$.ajax({
url: '/PerformanceSearch?handler=ResultsPartial',
data: JSON.stringify(dto),
dataType: 'json',
contentType: 'application/json',
type: 'POST',
success: function (data) {
$('#divPerformanceResults').html(data);
}
});
这是我试图将其映射到的模型:
public class RequestResultModel
{
public int ID { get; set; }
public int CODE { get; set; }
}
它是创建和返回分部视图的方法的参数,分部视图将包含所有过滤逻辑:
public PartialViewResult OnGetResultsPartial(RequestResultModel dto)
{
Results = new List<PerformanceResultModel>()
{
...
};
return new PartialViewResult
{
ViewName = "_PerformanceResults",
ViewData = new ViewDataDictionary<List<PerformanceResultModel>>(ViewData, Results)
};
}
该方法有效并且部分被渲染,所以所有这些都很好。这只是我需要开始工作的 dto,所以我可以过滤结果列表。我确实通过将方法参数切换为 int 来使以下内容起作用,但它只是一个参数,稍后我将需要多个输入。
$('#divPerformanceResults').load('/PerformanceSearch?handler=ResultsPartial', 'ID=15'); // This works. Only one param though
如果有任何提示,还附上 chrome 日志:
万千封印
ibeautiful
相关分类