2011-11-01 55 views
2

我有一個TreeView助手,它需要類別及其鏈接列表。我那樣做了

@helper TreeView(FavouriteLinksXmlMVC3.Controllers.HomeController cat) 
{ 
    cat = new FavouriteLinksXmlMVC3.Controllers.HomeController(); 
    try 
    { 
     foreach(FavouriteLinksXmlMVC3.Models.CategoriesControl list_category in cat.Categories) 
     { 
      <li> 
      <span class="folder" id="@list_category.Name">@list_category.Name</span> 

     @if(list_category.hasChild) 
     { 
       <ul> 
       @foreach(var links in list_category.Links) 
       { 
       <li><span class="file" id="@links.Url" categ_name="@list_category.Name">@links.Name</span></li> 
       } 
       </ul> 
     } 
     </li> 
     } 
     } 
     catch(Exception e) 
     { 
     Response.Write(e.ToString()); 
     }  
} 

好。這是一種魅力。

我做了一個函數,該函數使用jQuery的鏈接信息:

$(".file").click(function() { 
     $.ajax({ 
      url: '@Url.Action("GetLinkInfo")', 
      data: { cat_name: $(this).attr("categ_name"), url: $(this).attr("id") }, 
      type: "GET", 
      success: function (data) { 
       //alert(data.Name + " " + data.Url + " " + data.Description); 
       var make = "<table style='border-collapse:collapse' border='1'><tr><td>Name:</td><td>" + data.Name + "</td></tr><tr><td>Url:</td><td>" + data.Url + "</td></tr><tr><td>Description</td><td>" + data.Description + "</td></tr></table>"; 
       $("#details").html(make); 
      } 
     }); 
     }); 

而且GetLinkInfo

[HttpGet] 
     public ActionResult GetLinkInfo(string cat_name, string url) { 
      if (string.IsNullOrEmpty(cat_name)) 
      throw new ArgumentNullException("GetLinkInfo cat_name"); 
      if (string.IsNullOrEmpty(url)) 
      throw new ArgumentNullException("GetLinkInfo url"); 

      var c = this.Categories.Find(x => x.Name == cat_name); 
      string name1="", url1="", descr1=""; 
      bool done = false; 

      if (c != null) { 
      foreach (var p in c.Links) { 
       if (p.Url == url) { 
        name1 = p.Name; 
        url1 = p.Url; 
        descr1 = p.Description; 
        done = true; 
        break; 
       } 
      } 
      } 

      if (done) { 
      return Json(
       new { 
        Name = name1, 
        Url = url1, 
        Description = descr1 
       }, 
       JsonRequestBehavior.AllowGet 
       ); 
      } else { 
      return View(); 
      } 
     } 

我解決了這個問題,這

[HttpGet] //controller 
     public JsonResult GetCategoryInfo(string cat_name) { 
      if (string.IsNullOrEmpty(cat_name)) 
      throw new ArgumentNullException("GetCategoryInfo cat_name"); 

      var c = this.Categories.Find(x => x.Name == cat_name); 

      if (c != null) { 
      List<LinksControl> lk = null; 
      if (c.hasChild) { 
       lk = new List<LinksControl>(); 

       foreach (var p in c.Links) { 
        lk.Add(p); 
       } 
      } else { 
       lk = new List<LinksControl>(0); 
      } 
      return this.Json(lk,JsonRequestBehavior.AllowGet); 
      } else { 
      return this.Json(new List<LinksControl>(0),JsonRequestBehavior.AllowGet); 
      } 

     } 

而JQuery

$(".folder").click(function() { 
    var find_id = $(this).attr("id"); 

$.ajax({ 
      type: "GET", 
      url: '@Url.Action("GetCategoryInfo")', 
      dataType : 'json', 
      data: { cat_name: find_id }, 
      success: function (response) { 
      // $("#details").html(response.toString()); 


       var make = "<table style='border-collapse:collapse' border='1'>"; 
       make += "<tr><td>Name</td><td>Url</td><td>Description</td></tr>"; 

       $.each(response, function (index, lk) { 
       make += "<tr><td>" + lk.Name + "</td><td>" + lk.Url + "</td><td>" + lk.Description + "</td></tr>"; 
       }); 


       make += "</table>"; 
       $("#details").html(make); 
      } 
     }); 
      }); 
+0

這是什麼返回var c = this.Categories.Find(x => x.Name == cat_name);' – Rafay

+0

類別是一個泛型列表show'c'將返回一個CategoriesControl(它是一個類定義的類模型)數據類型與他的名字和他的鏈接列表。 –

+0

我更新了最後的代碼 –

回答

1

嘗試改變public ActionResult GetCategoryInfo()public JsonResult GetCategoryInfo()

我想,也許是ActionResult返回類型引起的問題,但是這僅僅是一種預感!

+0

同樣的事情,空的結果 –

+0

你是否已經通過了確保結果正在服務器上填充? – simonlchilds

+0

是與調試器,現在它工作。我再次更新了最新的兩個代碼(控制器和JQuery)。我錯過了'DataType:'json'' –

相關問題