c# - 布局页面的下拉列表 - MVC

标签 c# asp.net-mvc asp.net-mvc-4 razor

我的问题:布局页面的下拉列表。

我读过这篇文章:ASP.NET MVC Razor pass model to layout它或多或少与我的问题相似。
在其中一条评论中,Mattias Jakobsson 写道:“但一个常见的解决方案是使用 RenderAction 在布局页面中呈现需要自己数据的部分”。
好吧,我已经使用 @Html.Action() 创建了布局页面,该页面使用来自数据库的日期呈现我的 drop dwon 列表。一切都很完美。但...

  • 我有两个页面,例如:“主页”、“关于”和我在布局页面的下拉列表 (ddl)
  • 当我在“主页”并且我在 ddl 中更改选择时,如何实现它刷新“主页”页面,当我在“关于”时它刷新“关于”页面。
  • 如何通过页面存储选定的ddl值?
  • Layout.cshtml 的一部分代码:
        .
        .
        <body>
        <header id="top" class="grid-full-margin">
    
            <strong id="logo" class="grid-304"><a href="/"><img src="/images/logo.png" ></a></strong>
            @Html.ActionLink(@Resources.Resource.BackToIntranet, "Index", "Home", null, new {@class = "link link-home grid-position-left"})
    
            <h1>@Resources.Resource.SiteTitle</h1>
    
            <a href="#" class="link link-help">@Resources.Resource.LayoutHelp</a>
    
            <nav clss="grid-896">
    
                <ul>
                    <li>@Html.ActionLink(Resources.Resource.LayoutMenuItem1, "Index", "Home")</li>
                    <li>@Html.ActionLink(Resources.Resource.LayoutMenuItem2, "Index", "ClimaticStation")</li>
                    <li>@Html.ActionLink(Resources.Resource.LayoutMenuItem3, "Index", "ClimaticPoint")</li>
                    <li>@Html.ActionLink(Resources.Resource.LayoutMenuItem4, "Index", "IcewaterExchanger")</li>
                    <li>@Html.ActionLink(Resources.Resource.LayoutMenuItem5, "Index", "Pipeline")
                        <ul>
                            <li>@Html.ActionLink("Zestawienie", "YearsLength", "Pipeline")</li>
                        </ul>
                    </li>
                </ul>
    
                <div class="mod-select-list tbl-actions">
                    @Html.Partial("~/Views/Shared/Partials/LoginPartial.cshtml")
                </div>
            </nav>
    
    
    
        </header>
        <form action="#">
            @Html.Action("VariantsDdl", "MyBase")
        </form> 
    
        @RenderBody()
        .
        .
    
    MyBaseController.cs 的一部分
       public class MyBaseController : Controller
    {
       [ChildActionOnly]
        public ActionResult VariantsDdl()
        {
            var dataFromDb = GetDataFromDB(); // it's not importstn right now
            return this.PartialView("~/Views/Shared/Partials/VariantsDdlPartial.cshtml", dataFromDb);
        }
       .
       .
       }
    

    问候,
    马尔钦

    最佳答案

    好的,我已经设法解决了这个问题,我想知道您对我的解决方案的看法。

    _Layout.cshtml 看起来和第一篇文章一样,所以下面只是这个问题最重要的部分(布局下拉列表)

        <div style="float: right;">
                @Html.Action("VariantsDdl", "MyBase")
        </div>
    

    操作: VariantsDdl 在 MyBaseController 中实现。此操作从 session 中加载选定的变体 ID,如果它为 null,则从 web.config 加载(在这种情况下,项目要求必须在 db 中至少存在一个变体,并且必须在配置中指定其 ID):
        [ChildActionOnly]
        public ActionResult VariantsDdl()
        {
            long defaultVariantID;
            long.TryParse(System.Configuration.ConfigurationManager.AppSettings["DefaultVariantId"], out defaultVariantID);
    
            if (System.Web.HttpContext.Current.Session["mySelectedVariant"] != null)
            {
                long.TryParse(System.Web.HttpContext.Current.Session["mySelectedVariant"].ToString(), out defaultVariantID);
            }
    
            var variants = this.db.warianties.ToList();
            var items = new List<SelectListItem>();
            foreach (var variant in variants)
            {
                var selectedItem = false;
                if(variant.id == defaultVariantID)
                {
                    selectedItem = true;
                }
    
                items.Add(new SelectListItem { Selected = selectedItem, Text = variant.nazwa, Value = variant.id.ToString() });
            }
    
            return this.PartialView("~/Views/Shared/Partials/VariantsDdlPartial.cshtml", items);
        }
    

    将所选变体 ID 存储到 session 的部分查看和发布操作:
        @model IEnumerable<SelectListItem>
    
        <label for="field">Current variant</label>
        @Html.DropDownList("Varaints", Model, new { id = "variantsDdl" })
    
       <script type="text/javascript">
    $(function () {
        $('#variantsDdl').change(function () {
            var val = $('#variantsDdl').val()
            $.ajax({
                type: "POST",
                url: '@Url.Action("ChangeVariant", "MyBase")' + '/' + val,
                success: function (result) {
                    location.reload();
                },
                error: function (data) { alert('Error'); }
            });
    
        });
    });
    

    部分 View 发布操作“ChangeVariant”,将选定的变体 ID 保存到 session :
       [HttpPost]
        public ActionResult ChangeVariant(long id = 0)
        {
            System.Web.HttpContext.Current.Session["mySelectedVariant"] = id;
    
            return null;
        }
    

    这是我的要求的解决方案:
    1.布局时的DDL
    2. 在 DDL 'onchange' 刷新当前页面
    3.通过页面保持选定的DDL值

    请评论它是否是合适的解决方案,或者我应该采取不同的方式?

    问候,
    马尔钦

    关于c# - 布局页面的下拉列表 - MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21407508/

    相关文章:

    c# - 将 SQL 项目连接到数据库

    c# - 如何将上午/下午添加到日期时间

    c# - 更改语言中的 GRID.mvc 问题 [ASP.net MVC 4]

    asp.net-mvc-4 - MVC4 中的日期时间格式 - DisplayTemplates

    asp.net-mvc - 过滤器在asp.net mvc中执行的顺序是什么

    asp.net - 从 View 中的表中选择多个数据 asp.net MVC4 Razor

    c# - 有没有办法将 ConcurrentDictionary.TryUpdate 与 lambda 表达式一起使用?

    c# - C# 中的新类型,带有设计时检查

    c# - 衡量函数执行时间的最佳方法是什么?

    asp.net-mvc - ASP.Net MVC2 Controller 路径未找到 IIS7