区域持久侧栏MVC 3

本文关键字:MVC 区域 | 更新日期: 2023-09-27 18:03:38

我有一个区域在我的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,并在整个区域中持久化。

每个控制器中的每个Create, Edit和Details动作都需要传递这两个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)到他们相应的动作和控制器,并持续通过该区域的所有视图。

谢谢,

------ EDIT -------

好了,为了让它移动一点。我希望我将有一个Action,它将这两个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参数给部分视图和部分视图从哪里得到加载?

区域持久侧栏MVC 3

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

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

调用正确的Action
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 })
}

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

遗憾的是,这是一个按次查看的回答。

在布局页中定义一个节:

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

和菜单操作将看起来像问题中的一个。

。它比我在没有答案的情况下预期的更优雅。