题
我正在尝试从JQuery的控制器中调用服务器端操作:
$.ajax({
url:'http://localhost:88/admin/business/11/GetChildBusinessTypes',
data: { parentId: $('#business_parentbusinesstype_id').val() },
dataType: 'json',
success: fillChildBusinessTypes,
error: ajaxError
});
这是控制器操作:
public string GetChildBusinessTypes(int parentId)
{
//get child business types.
var businessTypes = BusinessTypeRepository.GetChildBusinessTypes(parentId);
//convert to JSON.
var serializer = new JavaScriptSerializer();
return serializer.Serialize(businessTypes);
}
它给我这个错误:
Monorail无法解析模板“ admin business getChildBusinessTypes'的视图引擎实例”,有两个可能的原因:要么不存在模板,要么尚未正确配置处理特定文件扩展名的视图引擎。节目viewEngines部分单轨)。
很明显,它正在尝试采取行动,就好像是一种观点和错误一样。我尝试将其作为帖子发送而不是get发送,但会收到相同的rror。我需要做什么才能使它工作?
谢谢!贾斯汀
解决方案
这是其他希望从jQuery调用控制器动作并返回JSON的人的答案。
控制器方法:
[return: JSONReturnBinder(Properties = "Id,Name")]
public BusinessType[] GetChildBusinessTypes(int parentId)
{
var businessTypes = BusinessTypeRepository.GetChildBusinessTypes(parentId);
return businessTypes;
}
JavaScript:
$(document).ready(function () {
$('#business_parentbusinesstype_id').change(function () {
jQuery.ajax({
url: "$UrlHelper.For("%{action='$business.site.id/GetChildBusinessTypes'}")",
data: { parentId: $('#business_parentbusinesstype_id').val() },
dataType: 'json',
type: 'GET',
success: fillChildBusinessTypes,
error: ajaxError
});
});
});
function fillChildBusinessTypes(json) {
//get business types.
var businessTypes = eval(json);
//bind business types to dropdown.
$("#business_businesstype_id").get(0).options.length = 0;
$("#business_businesstype_id").get(0).options[0] = new Option("Select a Business Type", "0");
jQuery.each(businessTypes, function(index, item) {
$('#business_businesstype_id').get(0).options[$("#business_businesstype_id").get(0).options.length] = new Option(item.Name, item.Id);
});
//show child dropdown.
Show($('#spnChildBusinessTypes'));
}
不隶属于 StackOverflow