1

我的 Web 应用程序中有一个区域。这个区域有几个控制器。有一个控制器负责处理该区域的入口(即一切都通过该控制器访问该区域中的其他控制器)。此控制器需要将 ID 传递给操作:

    public ActionResult Index(Guid appID)
    {
        var vm = new DeviceManagementViewModel
        {
            SelectedApp = appID,
            App = _repository.AUTs.FirstOrDefault(x => x.ID.Equals(appID)),
            Devices = GetDevices()
        };

        return View(vm);
    }

它创建的视图然后用另一个 ID 发回。因此,现在有两个 ID 已获得并在整个区域中持续存在。

每个控制器中的每个创建、编辑和详细信息操作都需要传递这两个 ID,以便它可以显示正确的数据,例如:

    public ActionResult Create(Guid appID, Guid deviceID)
    {
        var vm = new InputViewModel
        {
            Device = deviceID,
            Application = appID
        };

        return View(vm);
    }

现在,如果我可以有一个侧边栏,其中包含指向每个详细信息和创建操作的快速链接并填写相关 ID,那就太好了,例如:

@Html.ActionLink("Create Input Methods", "Create", "Input", new { appID = <-- ID ONE -->, deviceID = <-- ID TWO --> }, null)

而且,在整个区域中保留此侧边栏会更好,因此您可以快速切换每个控制器。我读过这个博客我想知道它是如何工作的,如果它是我正在寻找的?如果实际上有一种更简单的方法。

所以快速回顾一下,我需要一个侧边栏,它可以在输入这个特定区域(只能通过一个点输入)时动态更新,它将(使用额外参数传递两个 ID)链接到它们相应的动作和控制器和也贯穿该地区的所有观点。

谢谢,

- - - 编辑 - - - -

好的,帮助推动这一点。我希望我将有一个 Action 将这 2 个 ID 参数传递给:

public ActionResult Menu(Guid appID, Guid deviceID)
{
    var vm = MenuViewModel
    {
        DeviceID = deviceID,
        ApplicationID = appID
    };

    return PartialView(vm);
}

这将有一个像这样的 ViewModel:

public class MenuViewModel
{
    public Guid ApplicationID {get; set;}
    public Guid DeviceID {get; set;}
}

和像这样的部分视图:

<aside id="dashboard_menu">
<div id="extras">
    <div class="oi">
        <h2 class="screenreader-only">Menu</h2>

        <div class="rollup section">
            <h3 class="rollup-trigger">Input Methods</h3> 
            <p>View or Add more Input Methods</p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Input", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Input", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        </div>
        <div class="rollup section">
            <h3 class="rollup-trigger">Launch &amp; Exit</h3> 
            <p>View or Add more Launch &amp; Exit</p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Launch", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Launch", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        </div>
        <div class="rollup section">
            <h3 class="rollup-trigger">Performance</h3> 
            <p>View or Add more Performance</p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Performance", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Performance", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        </div>
        <div class="rollup section">
            <h3 class="rollup-trigger">Power Consumption</h3> 
            <p>View or Add more Power Consumption</p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Power", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Power", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        </div>
        <div class="rollup section">
            <h3 class="rollup-trigger">Qualitative Validation</h3> 
            <p>View or Add more Qualitative Validation</p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Qual", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
            <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Qual", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        </div>

    </div>
</div>

那么现在,如何使这个持久化,我是否在该区域的 _Layout.cshtml 中使用它?如果是这样怎么办?

如果是这样,我是否在每个视图页面中都使用它,我怎么能不这样做,因为它似乎比必要的工作更多?

我所困惑的是,您如何将这些 ID 参数传递给局部视图以及局部视图从何处加载?

4

2 回答 2

1

如果您需要能够为每个视图指定不同的侧边栏操作,那么您的解决方案非常棒;但是,如果您想要特定区域的侧边栏,这会在每个视图中增加不必要的开销和重复,如果有任何变化,这可能会使您的解决方案难以管理。

为避免这种情况,您可以尝试BaseController为每个区域创建一个,它指定侧边栏操作。然后,您可以确定视图中的当前区域并使用它来调用正确的操作RenderAction

public class SomeAreaBaseController : Controller
{
    public SomeAreaBaseController()
    {
        ViewBag.HasSidebar = true;
        ViewBag.Application = ...;
        ViewBag.Device = ...;
    }
    public ActionResult Menu(Guid appID, Guid deviceID)
    {
        ...
    }
}

现在,在此区域的其他控制器中继承此:

public class SomeController : SomeAreaBaseController

然后,在Layout.cshtml...

@if (ViewBag.HasSidebar) {
    @Html.RenderAction("Menu", ViewContext.RouteData.Values["controller"], 
        new { appID = ViewBag.Application, deviceID = ViewBag.Device })
}
于 2013-03-14T10:17:47.093 回答
0

所以,答案是在这个博客的帮助下:

不幸的是,这是一个按浏览页面的答案。

在布局页面中定义一个部分:

@if (IsSectionDefined("SideBar"))
{
    @RenderSection("SideBar")
}
else
{
    <div id="extras">
        <div class="oi">
            <h2 class="screenreader-only">Menu</h2>

            <div class="rollup section">
                <h3 class="rollup-trigger">Application Validation</h3> 
                <p>Goto all Applications</p>
                <p class="more"><a href="#">&raquo; @Html.ActionLink("All Applications", "Index", "Application", new { area = "Validation" }, null)</a></p>
            </div>
        </div>
    </div>   
}

然后在每个带有视图的页面上(我猜如果您首先创建此布局页面,那么视图将自动放入侧边栏部分供您填写)放入:

@section SideBar {
    @{Html.RenderAction("Menu", "DeviceManagement", new { appID = Model.Application, deviceID = Model.Device })}
}

在菜单部分视图内,将是:

<div id="extras">
<div class="oi">
    <h2 class="screenreader-only">Menu</h2>

    <div class="rollup section">
        <h3 class="rollup-trigger">Input Methods</h3> 
        <p>View or Add more Input Methods</p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Input", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Input", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
    </div>
    <div class="rollup section">
        <h3 class="rollup-trigger">Launch &amp; Exit</h3> 
        <p>View or Add more Launch &amp; Exit</p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Launch", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Launch", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
    </div>
    <div class="rollup section">
        <h3 class="rollup-trigger">Performance</h3> 
        <p>View or Add more Performance</p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Performance", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Performance", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
    </div>
    <div class="rollup section">
        <h3 class="rollup-trigger">Power Consumption</h3> 
        <p>View or Add more Power Consumption</p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Power", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Power", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
    </div>
    <div class="rollup section">
        <h3 class="rollup-trigger">Qualitative Validation</h3> 
        <p>View or Add more Qualitative Validation</p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("View", "Index", "Qual", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
        <p class="more"><a href="#">&raquo; @Html.ActionLink("Add", "Create", "Qual", new { appID = Model.ApplicationID, deviceID = Model.DeviceID}, null)</a></p>
    </div>

</div>

并且菜单操作看起来像问题中的那个。

完毕。在没有答案之后,它比我预期的更优雅。但这是我需要的。

于 2013-03-13T17:04:04.490 回答