【问题标题】:returning a partial view from a for loop从 for 循环返回部分视图
【发布时间】:2013-11-11 14:57:49
【问题描述】:

我的页面允许用户从复选框列表中选择企业并将这些企业插入到数据库中。

我使用 foreach 循环遍历表单集合中的每个项目,然后通过 db 上下文保存。

所有这些都运行良好。

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

在视图中,复选框列表以 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" />
                        }

此表单发布到操作,再次,一切运行良好。

我的问题是只有第一个条目被发布回屏幕。因此,如果复选框中仅选择了一个业务,则从控制器返回的部分视图将添加到 dom 中,但如果选择了多个业务,则仅显示第一个。

我猜那是因为控制器方法在第一次成功插入后“返回”,结束进程并且没有进行后续调用。

我唯一的困惑是所有业务都已成功插入。我希望这些也会失败,但刷新页面后,所有业务都正确显示。

【问题讨论】:

    标签: c# asp.net ajax entity-framework partial-views


    【解决方案1】:

    这里的问题是你的行为

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

    一旦您从请求结束的操作中return,ASP.NET 就无法理解您要遍历循环的其余部分。如果是这样,它将导致从服务器多次调用 & ,而这你可能无论如何都不想要。

    想想看,你的代码相当于

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

    这只会输出0。看起来你真正想要的是 yield 关键字

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

    我很确定你不能在 ASP.NET 中的 HTTP 请求上下文中做到这一点。

    解决这个问题的方法是在服务器端进行所有渲染。更改您的操作以将您的模型实际传递回某个容器视图

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

    然后在您的视图中,调用渲染每个部分

    @model System.Collections.Generic.List<Entity>
    
    @foreach (var entity in Model)
    {
         @Html.RenderPartial("_mypartialview", entity)
    }
    

    【讨论】:

    • 谢谢詹姆斯。我现在正在努力整合它。
    【解决方案2】:

    您不能从一个操作返回多个部分视图,就像您不能为一个 HTTP 请求接收多个 HTTP 响应一样。

    考虑更改您的局部视图以接受实体的集合,并在循环中呈现所有实体。

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 1970-01-01
      • 2018-09-07
      • 2019-07-31
      • 1970-01-01
      • 2021-12-17
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多