ASP.Net MVC 2 - 通过在参数中指示返回操作和控制器来重用控制器/视图

发布于 2024-10-11 08:25:23 字数 2060 浏览 0 评论 0原文

问题:

在我的应用程序中,我在多个位置为用户提供了一个选择屏幕(即,必须在多个操作中使用相同的选择屏幕)。

解决方案:

我提出了以下解决方案:将返回操作和控制器传递给处理实体选择的操作

示例:

假设应用程序中有多个位置用户必须选择 SomeEntity 的实例,因此我向该实体控制器添加了以下操作:

public class SomeEntityController : Controller
{
    /* ... */

    public ViewResult Select(string returnAction, string returnController)
    {
        var selectableEntities = ...;
        return View(
            new SelectionViewModel<SomeEntity>
            {
                Entities = selectableEntities,
                ReturnAction = returnAction,
                ReturnController = returnController,
            });
    }
}

在该操作的视图中 (Views/SomeEntity /Select.aspx)我这样写:

<table>
    <tr>
        <th>Select</th>
        <th>SomeProperty<th>
    </tr>
    <% foreach (var entity in Model.Entities) { %>
        <tr>
            <td>
                <%: Html.ActionLink("Select", Model.returnAction, Model.returnController, new { entityId = entity.id }) %>
            </td>
            <td><%: entity.SomeProperty %></td>
        </tr>
    <% } %>
</table>

然后,如果我需要用户在其他控制器中选择 SomeEntity 我可以这样做:

public class OtherController : Controller
{
    public ActionResult SelectSomeEntity()
    {
        return RedirectoToAction("Select", "SomeEntity", new { returnAction = "ActionThatNeedsAnEntity", returnController = "Other" });
    }

    public ActionResult ActionThatNeedsAnEntity(int entityId)
    {
        // here I can use the selected entity
    }
}

最后一段代码只是一个示例如何使用 SomeEntity 选择操作。除了 SelectSomeEntity 操作之外,还可以有一个更复杂的操作,该操作执行一些检查以查看 entityId 是否已被选择(例如存储在会话中),然后决定是否已选择是否调用 SomeEntity/Select

问题:

以上工作正常,但我是 ASP.Net MVC 2 的新手,所以我不知道是否还有其他(标准)解决方案为了这。

这种方法正确/简洁吗?您是否以不同的方式解决了同样的情况?

Problem:

In my application I have provide the user a selection screen for something in several places (that is, the same selection screen has to be used in several actions).

Solution:

I've come up with the following solution: pass the return action and controller to action which handles the entity selection.

Example:

Suppose that there are several places in the application where the user has to select an instance of SomeEntity, so I added the following action to that entity controller:

public class SomeEntityController : Controller
{
    /* ... */

    public ViewResult Select(string returnAction, string returnController)
    {
        var selectableEntities = ...;
        return View(
            new SelectionViewModel<SomeEntity>
            {
                Entities = selectableEntities,
                ReturnAction = returnAction,
                ReturnController = returnController,
            });
    }
}

In the view for that action (Views/SomeEntity/Select.aspx) I put something like this:

<table>
    <tr>
        <th>Select</th>
        <th>SomeProperty<th>
    </tr>
    <% foreach (var entity in Model.Entities) { %>
        <tr>
            <td>
                <%: Html.ActionLink("Select", Model.returnAction, Model.returnController, new { entityId = entity.id }) %>
            </td>
            <td><%: entity.SomeProperty %></td>
        </tr>
    <% } %>
</table>

Then, if I need the user to select a SomeEntity in other controller I can do this:

public class OtherController : Controller
{
    public ActionResult SelectSomeEntity()
    {
        return RedirectoToAction("Select", "SomeEntity", new { returnAction = "ActionThatNeedsAnEntity", returnController = "Other" });
    }

    public ActionResult ActionThatNeedsAnEntity(int entityId)
    {
        // here I can use the selected entity
    }
}

The last piece of code is just an example of how to use the SomeEntity selection action. Instead of the SelectSomeEntity action, there could be a more complex action which performs some checks to see if an entityId is already selected (e.g. stored in the session) and then decide whether to call SomeEntity/Select or not

Question:

The above works fine, but I’m new to ASP.Net MVC 2 so I don’t know if there is other (standard) solution for this.

Is this approach correct/neat? Have you solve this same situation differently?

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

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

发布评论

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

评论(1

烦人精 2024-10-18 08:25:23

我可能会误解您的问题,但我认为部分视图将是您正在寻找的“标准”解决方案。

部分视图就是可以插入到其他视图中的小视图。输入表单或数据显示等内容可以放入部分视图中并添加到常规视图中。它们极大地简化了代码。

它们制作起来很简单。当您要创建常规视图时,只需选中窗口中的“部分视图”框(即在解决方案资源管理器中右键单击并选择“添加视图”选项后)。然后,您可以使用 <%: Html.Partial("myPartialView") %> 将其放入任何完整视图中。您也可以通过执行 <%: Html.Partial("myPartialView", Model) %> 轻松地将模型传递给部分视图

I could be misunderstanding your problem, but I think that Partial Views would be the "standard" solution you're looking for.

Partial Views are just that, small views that can be inserted into other views. Things like entry forms or displays of data can be put into a partial view and added to the regular view. They greatly simplify code.

They're simple to make. When you go to make a regular view, just check the "partial view" box in the window (i. e. after right-clicking in the solution explorer and selecting the "add view" option). You can then throw this into any full view by using <%: Html.Partial("myPartialView") %>. You can easily pass a Model to the partial view as well by doing <%: Html.Partial("myPartialView", Model) %>

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