如何在 asp.net mvc3 中将对象列表从视图传递到控制器操作

发布于 2024-11-11 17:02:32 字数 2207 浏览 3 评论 0原文

我进行了全面的搜索,返回了多个对象列表。每个这样的列表都由包含附加列表的对象组成。就处理器负载而言,搜索非常复杂。

一旦我得到结果,我就会通过部分视图显示原始对象。

  public ActionResult BeginSearch(SearchHomeVM searchParameters)
      {
         var search = new Search(searchParameters);
         linije = search.PretraziLinije();

         return PartialView("_searchResult", linije);
       }

然后在该表单中我希望通过 AJAX 调用显示特定项目的详细信息。问题是我需要使用对象数据,而不是在数据库中运行另一个搜索。 在剃刀中,我有:

  @model LinijeSearchResult
  @if (Model.BrojDirektnihLinija > 0)
  {
  <table id="direktneLinije" class="InvisibleTable">
    <thead>
        <tr>
            <th>
                Direktne linije
            </th>
            <th>
            </th>
        </tr>
    </thead>
    @for (int index = 0; index < Model.DirektneLinije.Count; index++)
    {
        LinijaSM item = Model.DirektneLinije[index];
        List<LinijaSM> lin = new List<LinijaSM> { item };
        <tr>
            <td>@item.Naziv
            </td>
            <td>
                @using (Ajax.BeginForm("RenderStanice",
                    new { psd = 0, index = index, lin = lin },
                    new AjaxOptions
                    {
                        HttpMethod = "POST",
                        UpdateTargetId = "staniceLinije",
                        InsertionMode = InsertionMode.Replace
                    }))
                {
                    <input type="submit" value="Stanice" />
                }
            </td>
        </tr>
    }
   </table>

   }
   else
   {
    <text>Nema direktnih linija za odabrane parametre.</text>
    <br />
   }

这里您可以看到我如何尝试将数据传递到控制器操作,如下所示:

    public ActionResult RenderStanice(List<LinijaSM> lin)
    {
        return PartialView("_staniceSR", lin);
    }

在该控制器操作中,我得到空列表。 请您建议如何实现这一点。

<强><<编辑>>

到目前为止我已经弄清楚,任何对象的列表都不能传递回控制器。对于复杂的对象也是如此。我可以传递一个整数,但不能传递一个整数列表。

有人可以建议我如何实现我的目标吗? 我需要将对象列表传递回控制器。可以通过将其添加到上下文、创建新的 viewData 或类似的方式来完成吗?

如果这是不可能的,是否可以通过 AJAX 渲染部分视图,但无需控制器操作?

i have comprehensive search that returns several lists of objects. Each such list is made of objects containing additional lists. The search is very complex in terms of processor load.

once i have the results, i display the original objects via partial view.

  public ActionResult BeginSearch(SearchHomeVM searchParameters)
      {
         var search = new Search(searchParameters);
         linije = search.PretraziLinije();

         return PartialView("_searchResult", linije);
       }

then in that form i wish to display details for a particular item via AJAX call. The problem is i need to use the objects data, not run another search in database.
In razor i have:

  @model LinijeSearchResult
  @if (Model.BrojDirektnihLinija > 0)
  {
  <table id="direktneLinije" class="InvisibleTable">
    <thead>
        <tr>
            <th>
                Direktne linije
            </th>
            <th>
            </th>
        </tr>
    </thead>
    @for (int index = 0; index < Model.DirektneLinije.Count; index++)
    {
        LinijaSM item = Model.DirektneLinije[index];
        List<LinijaSM> lin = new List<LinijaSM> { item };
        <tr>
            <td>@item.Naziv
            </td>
            <td>
                @using (Ajax.BeginForm("RenderStanice",
                    new { psd = 0, index = index, lin = lin },
                    new AjaxOptions
                    {
                        HttpMethod = "POST",
                        UpdateTargetId = "staniceLinije",
                        InsertionMode = InsertionMode.Replace
                    }))
                {
                    <input type="submit" value="Stanice" />
                }
            </td>
        </tr>
    }
   </table>

   }
   else
   {
    <text>Nema direktnih linija za odabrane parametre.</text>
    <br />
   }

Here You can see how i am trying to pass the data to controller action that looks like this:

    public ActionResult RenderStanice(List<LinijaSM> lin)
    {
        return PartialView("_staniceSR", lin);
    }

In that controller action i get the empty list.
can You please advise on how to accomplish this.

<< EDIT >>

up to this point i have figured out, that the List of any object can not be passed back to controller. the same goes for complex objects. I can pass a integer, but not a list of integers.

Can someone advise me on how to accomplish my goal?
I need to pass a list of object back to controller. Can it be done by adding it to context, creating new viewData or something like that?

If that is not possible, can a partial view be rendered via through AJAX, but without a controller action?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-18 17:02:32

看来您应该在 For 循环之外定义列表,然后在循环中添加到它。否则,您每次都会定义一个新列表,并且只能从最后一次通过 for 得到列表的样子,而 for 可能什么也没有,因此您的列表为空。

更新:但是......更重要的是,您应该在将其传递到部分视图之前在控制器中构建一个视图就绪对象。使用 Razor 在视图中创建该对象并不是很好的关注点分离。创建一个视图模型以发送到包含该列表的完整视图,然后将该列表传递到您的部分视图。

It seems like you should probably be defining your list outside of the For loop and then adding to it in the loop. Otherwise, you'll be defining a new list every time, and only get what the list looks like from the last pass through the for which could be nothing, hence your empty list.

Update: But... even more important, you should be building a view-ready object in the controller before you're passing it to your partial view. Making that object in the view using Razor isn't really very good separation of concerns. Make a view model to send to your full view that contains that list and then just pass that list to your partial view.

流云如水 2024-11-18 17:02:32

我目前解决这个问题的方法是将数据暂时持久化在Session中。

        linije = search.PretraziLinije();
        Session["direktneLinije"] = linije.DirektneLinije;
        Session["jednoPresjedanjeLinije"] = linije.LinijeUzJednoPresjedanje;
        Session["dvaPresjedanjaLinije"] = linije.DirektneLinije;

并仅来回传递字段的索引以访问适当的数据。

    @for (int index = 0; index < Model.DirektneLinije.Count; index++)
    {
        LinijaSM item = Model.DirektneLinije[index];
        <tr>
            <td>@item.Naziv
            </td>
            <td>
                @using (Ajax.BeginForm("RenderStanice",
                    new { index, bpr = 0 },
                    new AjaxOptions
                    {
                        HttpMethod = "POST",
                        UpdateTargetId = "staniceLinije",
                        InsertionMode = InsertionMode.Replace
                    }))
                {

                    <input type="submit" value="Stanice" />
                }
            </td>
        </tr>
    }

我不确定这是一个非常好的方法,但由于它是相当少量的数据,被访问几次然后变得无关紧要,因此再次计算它或将其保存在数据库中的成本会更高。

如果有任何人可以提供更好的解决方案,请提供。

My current solution to this problem is to persist the data temporarily in Session.

        linije = search.PretraziLinije();
        Session["direktneLinije"] = linije.DirektneLinije;
        Session["jednoPresjedanjeLinije"] = linije.LinijeUzJednoPresjedanje;
        Session["dvaPresjedanjaLinije"] = linije.DirektneLinije;

and pass back and forth only index of the field to access the appropriate data.

    @for (int index = 0; index < Model.DirektneLinije.Count; index++)
    {
        LinijaSM item = Model.DirektneLinije[index];
        <tr>
            <td>@item.Naziv
            </td>
            <td>
                @using (Ajax.BeginForm("RenderStanice",
                    new { index, bpr = 0 },
                    new AjaxOptions
                    {
                        HttpMethod = "POST",
                        UpdateTargetId = "staniceLinije",
                        InsertionMode = InsertionMode.Replace
                    }))
                {

                    <input type="submit" value="Stanice" />
                }
            </td>
        </tr>
    }

I am not sure that this is a very good approach, but since it is a reasonably small amount of data that gets accessed couple of times and then becomes irrelevant, it would be more costly to compute it again or persist it in database.

If any1 can offer a better solution, please do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文