C# MVC 4 控制器 JsonResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16686367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:39:11  来源:igfitidea点击:

MVC 4 Controller JsonResult

c#ajaxasp.net-mvcjson

提问by Stefan v.d Laarschot

I have a question related to my controller in MVC. I want to loop inside my JsonResult GetAfmeldingenusing foreach.

我有一个与 MVC 中的控制器相关的问题。我想GetAfmeldingen使用 foreach在我的 JsonResult 中循环。

But what I do will not go inside my foreach here is my code as it looks right now

但是我所做的不会进入我的 foreach 这里是我现在看起来的代码

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);
    }
}

In this example, I return a JsonResultwhen 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.

在此示例中,JsonResult当我的列表中的第一条记录命中时,我返回 a 。我已经看了看它是否有效,但没有。id 已填写,但我在这里缺少什么?我是 MVC 的新手。

采纳答案by Nick N.

This is probably what doesn't work, since you cannot come into the foreach.

这可能是行不通的,因为您无法进入 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 }
        );

回答by Robert McKee

This will terminate on the first loop of the foreach, is that what you want?

这将在 foreach 的第一个循环中终止,这是您想要的吗?

            foreach (var item in Talent.Afmelding.Fetch(null, Models.Taken.ActID, null, null))
            {

                return Json(item.Participant.CompleteName, JsonRequestBehavior.AllowGet);
            }