2010-05-27 22 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')); 
    }