MVC 3 Action 在不同的控制器上,但需要 ViewModel

发布于 2024-11-04 17:56:22 字数 1304 浏览 1 评论 0原文

我正在寻找解决以下问题的最佳方法:我有一个搜索页面,可以选择不同的条件,这些条件在 SearchViewModel 中可用。现在,搜索返回 SearchResults 列表,这些结果是产品。这些可以直接添加到购物篮中。 现在的问题是,将商品添加到购物篮的操作是在 BasketController 而不是 SearchController 上。当用户单击它时,他将返回到搜索页面,但会丢失所做的所有选择。

if (Request.UrlReferrer != null)
        return Redirect(Request.UrlReferrer.ToString());

我尝试使用 TempData 解决该问题,但出现“查询结果不能多​​次枚举”的异常。

private SearchViewModel EnsureViewModel(SearchViewModel viewModel)
    {
        if (TempData["SearchModel"] != null && viewModel.SearchResult == null)
            viewModel = TempData["SearchModel"] as SearchViewModel;

        TempData["SearchModel"] = viewModel;
        return viewModel;
    }

在这里,我在模型中显示结果列表,抛出异常:

@Html.DisplayFor(p => Model.SearchResult)

我还考虑过可能有一种方法可以使用部分视图来执行此操作? 当然,我的最后一个资源只是复制代码,但我对这个想法并不感到兴奋......

任何想法都表示赞赏,谢谢=)


编辑 使用以下形式调用控制器:

@using (Html.BeginForm("Add", "Basket", new { id = Model.Name } ))
    {
        @Html.TextBox("amount", "1", new { name="amount", maxlength=7, @class = "txtfield number" })
        <button><img src="@Url.Content("~/Content/images/icon_basket.gif")" border="0" width="14" height="10" class="basket" /></button>
    }       

I'm searching for the best possible way to solve the following problem: I have a search page, with different criterias that can be selected, which are availible in the SearchViewModel. Now the search returns a list of SearchResults, which are products. Those can be added directly to the basket.
Now the problem is, the action for adding the item to the basket is on the BasketController and not the SearchController. When the user clicks it, he is returned to the SearchPage but loses all the selections he has made.

if (Request.UrlReferrer != null)
        return Redirect(Request.UrlReferrer.ToString());

I have tried working around the problem using TempData, but I get a "The result of a query cannot be enumerated more than once."-Exception.

private SearchViewModel EnsureViewModel(SearchViewModel viewModel)
    {
        if (TempData["SearchModel"] != null && viewModel.SearchResult == null)
            viewModel = TempData["SearchModel"] as SearchViewModel;

        TempData["SearchModel"] = viewModel;
        return viewModel;
    }

Here where I display the list of my results in the model, the exception is thrown:

@Html.DisplayFor(p => Model.SearchResult)

I've also considered that there might be a way to do this using a partial view?
Of course, my last ressource is just duplicating the code, but I'm not thrilled by that idea...

Any ideas are appreciated, thanks =)


Edit
The call for the controller is made using the form as following:

@using (Html.BeginForm("Add", "Basket", new { id = Model.Name } ))
    {
        @Html.TextBox("amount", "1", new { name="amount", maxlength=7, @class = "txtfield number" })
        <button><img src="@Url.Content("~/Content/images/icon_basket.gif")" border="0" width="14" height="10" class="basket" /></button>
    }       

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

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

发布评论

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

评论(3

意犹 2024-11-11 17:56:22

我认为重定向是导致 TempData 崩溃的原因,尝试将控制器代码中的“重定向”替换为“View”,并向其传递视图名称(而不是整个 url)。

(注意:如果您的控制器操作正在执行任何操作来填充视图模型,您需要复制该代码并将其插入到调用视图之前(我将其提取到私有方法中并从两个地方调用它))

I think the Redirect is what's blowing up the TempData, try replacing "Redirect" in your controller code with "View" and pass it the view name (not the whole url).

(Note: if your controller action was doing anything to populate a view model you'll need to copy that code and insert it just before your call to the View (I pull it out into a private method and call it from both places))

你的往事 2024-11-11 17:56:22

为什么不使用 AJAX 调用“添加到购物篮”方法?然后,用户无需离开您的搜索页面即可将商品添加到购物篮,这将保留他们的搜索。

另外,当我实现搜索参数时,我经常在查询字符串上传递这些参数。它们仍然填充 SearchViewModel,但将它们放在查询字符串上可以让它们在必要时在 Web 请求之间轻松传递。

Why not call the "Add to Basket" method using AJAX? Then users don't need to leave your search page to add items to the basket, which will preserve their search.

Also, when I implement search parameters I often pass those parameters on the query string. They still populate a SearchViewModel, but having them on the query string allows them to be passed around between web requests pretty easily if necessary.

如果您无法使用 AJAX,还有另一个选择:

  1. 将所有购物篮项目存储到会话中(显然)。
  2. 呈现搜索结果页面时,对于每个项目验证它是否已放入购物篮中。如果是,请显示已选中。
  3. 当用户点击“添加到购物篮”时,清除您的购物篮并使用最后一篇文章重新填充它。

当然,我不确定你是否使用分页。如果是的话,这个解决方案可能行不通......

If you can't use AJAX, there is another option:

  1. Store all basket items into the session (obviously).
  2. When rendering the search result page, for each item verify if it is already into the basket. If it is, show it checked.
  3. When the user clicks on "Add to basket", clear you basket and repopulate it using this last post.

Of course, I'm not sure if you're using pagination. If you are, this solution probably will not work...

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