返回一个JSON数组$就从的ActionResult类型的方法在MVC 3 [英] Return a JSon array to $.ajax from ActionResult type method in MVC 3
本文介绍了返回一个JSON数组$就从的ActionResult类型的方法在MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个TreeView帮手这需要类别及其链接的列表。我这样做,以
@helper TreeView控件(FavouriteLinksXmlMVC3.Controllers.HomeController猫)
{
猫=新FavouriteLinksXmlMVC3.Controllers.HomeController();
尝试
{
的foreach(FavouriteLinksXmlMVC3.Models.CategoriesControl list_category在cat.Categories)
{
<李>
<跨度类=文件夹ID =@ list_category.Name> @ list_category.Name< / SPAN>
@if(list_category.hasChild)
{
< UL>
@foreach(在list_category.Links VAR链接)
{
<李><跨度类=文件ID =@ links.Urlcateg_name =@ list_category.Name> @ links.Name< / SPAN>< /李>
}
< / UL>
}
< /李>
}
}
赶上(例外五)
{
回复于(e.ToString());
}
}
好。它的工作就像一个魅力。
我做了一个函数,它接受一个链接信息与JQuery的:
$(文件)。点击(函数(){
$阿贾克斯({
网址:@ Url.Action(GetLinkInfo),
数据:{cat_name:$(本).attr(categ_name),网址:$(本).attr(ID)},
键入:GET,
成功:功能(数据){
//alert(data.Name ++ data.Url ++ data.Description);
VAR化妆=<表样式='边界崩溃:崩溃的边界='1'>< TR>< TD>名称:< / TD>< TD>中+ data.Name +< / TD>< / TR>< TR>< TD>地址:LT; / TD>< TD>中+ data.Url +< / TD>< / TR>< TR>< TD>简介< / TD>< TD>中+ data.Description +< / TD>< / TR>< /表>;
$(#详细信息)HTML(做)。
}
});
});
而GetLinkInfo
[HTTPGET]
公众的ActionResult GetLinkInfo(字符串cat_name,字符串URL){
如果(string.IsNullOrEmpty(cat_name))
抛出新ArgumentNullException(GetLinkInfo cat_name);
如果(string.IsNullOrEmpty(URL))
抛出新ArgumentNullException(GetLinkInfo URL);
变种C = this.Categories.Find(X => x.Name == cat_name);
串NAME1 =,URL1 =,descr1 =;
布尔做= FALSE;
如果(C!= NULL){
的foreach(在c.Links VAR P){
如果(p.Url == URL){
NAME1 = p.Name;
为url1 = p.Url;
descr1 = p.Description;
做=真;
打破;
}
}
}
如果(完成){
返回JSON(
新 {
名称=名称1,
URL = URL1,
说明= descr1
},
JsonRequestBehavior.AllowGet
);
} 其他 {
返回查看();
}
}
我解决了这个问题,这个
[HTTPGET] //控制器
公共JsonResult GetCategoryInfo(字符串cat_name){
如果(string.IsNullOrEmpty(cat_name))
抛出新ArgumentNullException(GetCategoryInfo cat_name);
变种C = this.Categories.Find(X => x.Name == cat_name);
如果(C!= NULL){
名单< LinksControl> LK = NULL;
如果(c.hasChild){
LK =新的名单,其中,LinksControl>();
的foreach(在c.Links VAR P){
lk.Add(对);
}
} 其他 {
LK =新的名单,其中,LinksControl>(0);
}
返回this.Json(LK,JsonRequestBehavior.AllowGet);
} 其他 {
返回this.Json(新名单,其中,LinksControl>(0),JsonRequestBehavior.AllowGet);
}
}
和JQuery的
$(文件夹)。点击(函数(){
VAR find_id = $(本).attr(ID);
$阿贾克斯({
键入:GET,
网址:@ Url.Action(GetCategoryInfo),
数据类型:JSON,
数据:{cat_name:find_id},
成功:函数(响应){
// $(#详细信息)HTML(response.toString());
VAR化妆=<表样式='边界崩溃:崩溃的边界=1>中;
使+ = "<tr><td>Name</td><td>Url</td><td>Description</td></tr>";
$每个(响应函数(指数,LK){
使+ =&LT; TR&GT;&LT; TD&gt;中+ lk.Name +&所述; / TD&GT;&其中; TD&gt;中+ lk.Url +&所述; / TD&GT;&其中; TD&gt;中+ lk.Description +&所述; / TD&GT;&所述; / TR&gt;中;
});
使+ =&LT; /表&gt;;
$(#详细信息)HTML(做)。
}
});
});
解决方案
试着改变公众的ActionResult GetCategoryInfo()
到公共JsonResult GetCategoryInfo()
我想,也许是的ActionResult
返回类型是造成问题,但是这仅仅是一种预感!
I have a TreeView helper which takes list of categories and its links. I did that with
@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() );
}
}
Good. It's work like a charm.
I did a function which takes a link information with 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);
}
});
});
And the 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();
}
}
I solved the problem with this
[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);
}
}
And the 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);
}
});
});
解决方案
Try changing public ActionResult GetCategoryInfo()
to public JsonResult GetCategoryInfo()
I think maybe the ActionResult
return type is causing problems, but this is just a hunch!
这篇关于返回一个JSON数组$就从的ActionResult类型的方法在MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文