2010-05-27 47 views
0

我试图调用从jQuery的控制器中的一个服务器端的操作调用服务器端方法:单轨 - 如何从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'的视图引擎实例有两种可能的原因:模板不存在,或处理特定文件扩展名的视图引擎尚未正确配置web.config(节单轨,节点viewEngines)。

很明显,它试图将操作看作是查看和错误输出。我尝试将它作为POST发送而不是GET,但收到相同的错误。我需要做些什么来实现这个目标?

谢谢! 贾斯汀

回答

1

这里的其他人谁正在寻找调用从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')); 
    }