ASP.Net MVC 2 - 通过指示在PARAMS行动回报和控制器重用控制器/视图 [英] ASP.Net MVC 2 - reusing Controller/View by indicating return Action and Controller in params

查看:101
本文介绍了ASP.Net MVC 2 - 通过指示在PARAMS行动回报和控制器重用控制器/视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序具有用户在几个地方的东西(即,相同的选择画面有在几个动作被使用)提供的选择屏幕。

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).

我已经想出了以下解决方案:传递返回动作和控制器的动作它处理的实体选择

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

假设有在用户有选择 SomeEntity 的一个实例的应用程序的几个地方,所以我增加了以下行动到实体控制器:

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

在该动作视图(查看/ SomeEntity / Select.aspx)我把这样的事情:

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>

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

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
    }
}

code的最后一块只是如何使用 SomeEntity 选择行动的例子。相反, SelectSomeEntity 的动作,有可能是一个更复杂的行动而进行一些检查,以查看是否有 ENTITYID 已经选择(例如,保存在会话),然后再决定是否调用 SomeEntity /选择或不

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

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

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.

这是正确的方法/整齐?你有不同的解决这个同样的情况?

推荐答案

我可以误解你的问题,但我认为这部分意见将是你正在寻找的标准解决方案。

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

局部视图是正义的,可以插入其他视图小意见。像报名表或数据显示的东西都可以放到一个局部视图,并添加到普通视图。他们大大简化code。

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.

他们制作简单。当你做一个普​​通视图,只需勾选局部视图对话框窗口(即后在Solution Explorer中右击并选择添加视图选项)。 Html.Partial(myPartialView)%&GT; :%;然后,您可以通过使用&LT扔到任何众目睽睽这一点。你可以很容易地通过一个模型的局部视图,以及通过做&LT;%:Html.Partial(myPartialView模型)%GT;

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) %>

这篇关于ASP.Net MVC 2 - 通过指示在PARAMS行动回报和控制器重用控制器/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆