简体   繁体   English

从for循环返回局部视图

[英]returning a partial view from a for loop

My page allows the user to select businesses from a checkbox list and have those businesses inserted into the db. 我的页面允许用户从复选框列表中选择企业,并将这些企业插入数据库。

I use a foreach to loop through each item in the form collection and then save through the db context. 我使用foreach遍历表单集合中的每个项目,然后通过db上下文保存。

All of this is working well. 所有这些都运行良好。

public ActionResult RecordBusinesses(FormCollection collection)
    {
        foreach (var item in collection.GetValues("mycheckboxlist"))
        {
            modelentity modelentity = new modelentity();

            if (ModelState.IsValid)
            {
                db.modelentity.Add(modelentity);
                db.SaveChanges();
            }

            return PartialView("_mypartialview", modelentity);
        }
        return PartialView("_mypartialview");
    }

In the view, the checkbox list is wrapped in an ajax form. 在视图中,复选框列表以ajax形式包装。

@using (Ajax.BeginForm(
                        "RecordBusinesses",
                        new AjaxOptions
                        {
                        HttpMethod = "POST",
                        InsertionMode = InsertionMode.InsertBefore,
                        UpdateTargetId = "insertedbusiness"
                        }))
                        {

                            foreach(var b in Model)
        {
            <input type="checkbox" name="mycheckboxlist" value="@b.businessid">@b.name
        <br />
        }
                            </div>
                            <input type="submit" />
                        }

This form posts to the action and, again, everything is working well. 此表单将发布到操作中,并且再次一切正常。

My problem is that only the first entry is posted back to the screen. 我的问题是只有第一个条目被发布回屏幕。 So the partialview that is returned from the controller is added to the dom if there's only one business selected in the checkbox, but if there's multiple businesses selected, only the first one shows up. 因此,如果复选框中仅选择了一个业务,则从控制器返回的partialview将添加到dom中,但是如果选择了多个业务,则仅显示第一个业务。

I'm guessing thats because the controller method "returns" after the first successful insert, ending the process and subsequent calls are not made. 我猜那是因为控制器方法在第一次成功插入后会“返回”,因此不会结束进程和后续调用。

My only confusion there is that all of the businesses are successfully inserted. 我唯一的困惑是所有业务都已成功插入。 I would expect those to fail as well, but upon refreshing the page, all businesses are displayed correctly. 我希望这些也会失败,但是刷新页面后,所有业务都会正确显示。

The issue here is your action 这里的问题是你的行动

public ActionResult RecordBusinesses(FormCollection collection)
{
    foreach (var item in collection.GetValues("mycheckboxlist"))
    {
        ...
        return PartialView("_mypartialview", modelentity);
    }
    return PartialView("_mypartialview");
}

Once you return from the action the request ends, ASP.NET doesn't understand that you want to iterate through the rest of the loop. 从操作return后,请求结束,ASP.NET无法理解您要遍历循环的其余部分。 If it did, it would result in multiple calls to & from the server which you probably wouldn't want anyway. 如果这样做的话,将导致服务器可能多次从&调用您可能仍然不希望的。

Think about it, your code is the equivalent to 考虑一下,您的代码等效于

for(int i = 0; i <= 10; i++)
{
    Console.WriteLine(i);
    return i;
}

This would only ever output 0 . 这只会输出0 It looks like what you are really after here is the yield keyword 看起来您真正想要的是yield关键字

for (int i = 0; i <= 10; i++)
{
    yield return i;
}

Which I am pretty sure you can't do in ASP.NET in the context of an HTTP request. 我敢肯定,在HTTP请求的上下文中,您不能在ASP.NET中执行此操作。

The way around this is to do all your rendering server side. 解决此问题的方法是完成所有渲染服务器端。 Change your action to actually pass your model back to some container view 更改操作以将模型实际返回到某些容器视图

public ActionResult RecordBusinesses(FormCollection collection)
{
    List<Entity> myEntities = ...;
    foreach (var item in collection.GetValues("mycheckboxlist"))
    {
        ...
        myEntities.Add(new ...);
    }
    return View("SomeContainerView", myEntities);
}

Then in your view, make the calls to render each partial 然后在您的视图中,进行调用以渲染每个部分

@model System.Collections.Generic.List<Entity>

@foreach (var entity in Model)
{
     @Html.RenderPartial("_mypartialview", entity)
}

You cannot return multiple partial views from one action, in the same sense as you cannot receive multiple HTTP responses for one HTTP request. 您无法从一个操作返回多个局部视图,就如同您无法为一个HTTP请求接收多个HTTP响应一样。

Consider changing your partial view to accept a collection of entities and render all of them in a loop. 考虑更改局部视图以接受实体的集合并在一个循环中呈现所有实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM