问题描述
我有一个与MVC中的控制器有关的问题.我想使用foreach在我的JsonResult GetAfmeldingen
中循环.
I have a question related to my controller in MVC.I want to loop inside my JsonResult GetAfmeldingen
using foreach.
但是我做的事情不会深入我的foreach这是我现在的代码
But what I do will not go inside my foreachhere is my code as it looks right now
public JsonResult GetJsonAfmeldingen()
{
if (Functions.HasLoginCookie())
{
if (Models.Taken.ActID > 0)
{
foreach (var item in Talent.Afmelding.Fetch(null, Models.Taken.ActID, null, null))
{
return Json(item.Participant.CompleteName, JsonRequestBehavior.AllowGet);
}
return Json("Empty ?? ID = " + Models.Taken.ActID + "", JsonRequestBehavior.AllowGet);
}
else
{
return Json(null, JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(null, JsonRequestBehavior.AllowGet);
}
}
在此示例中,当列表中的第一条记录命中时,我返回JsonResult
.我一直在看是否可行,但没有成功. ID已填满,但我在这里想念的是什么?我是MVC的新手.
In this example, I return a JsonResult
when the first record in my list hits. I've looked to see if it works but it doesn't. The id is filled but what am I missing here?I am new to MVC.
推荐答案
这可能不起作用,因为您无法进入foreach.
This is probably what doesn't work, since you cannot come into the foreach.
public JsonResult GetJsonAfmeldingen()
{
if (Functions.HasLoginCookie())
{
//This one is probably not filled.
if (Models.Taken.ActID > 0)
{
这可能是一种解决方案:
This could be a possible solution:
public JsonResult GetJsonAfmeldingen(int actID)
{
if (Functions.HasLoginCookie())
{
//Now it is filled if you post it correctly.
if (actID > 0)
{
然后尝试在较小的范围内对其进行测试.尝试在此行上有一个断点,看看列表是否已填充.
After that try to test it in a smaller scope. Try to have a breakpoint on this line and see if the list is filled.
var listAfmeldingen = Talent.Afmelding.Fetch(null, Models.Taken.ActID, null, null);
//Breakpoint here
int count = listAfmeldingen.Count;
foreach (var item in listAfmeldingen)
{
return Json(item.Participant.CompleteName, JsonRequestBehavior.AllowGet);
}
也请看一下您的路由,我使用这个:
Have a look at your routing as well, I use this one:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这篇关于MVC 4控制器JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!