返回一个JSon数组到$.ajax从ActionResult类型的方法在MVC 3

本文关键字:类型 ActionResult 方法 MVC ajax 一个 数组 JSon 返回 | 更新日期: 2023-09-27 18:12:51

我有一个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);
           }
        });
          });

返回一个JSon数组到$.ajax从ActionResult类型的方法在MVC 3

尝试将public ActionResult GetCategoryInfo()更改为public JsonResult GetCategoryInfo()

我想也许是ActionResult返回类型引起的问题,但这只是一种预感!