布局页面上的下拉列表-MVC [英] Drop down list at layout page - MVC

查看:69
本文介绍了布局页面上的下拉列表-MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:在布局页面上下拉列表.

My problem: drop down list at layout page.

我已经阅读了这篇文章: ASP.NET MVC Razor pass模型来进行布局,这与我的问题或多或少相似. Mattias Jakobsson在评论中写道:但是,常见的解决方案是使用RenderAction在布局页面中呈现需要自己数据的零件". 好吧,我已经用@ Html.Action()创建了一个布局页面,该页面呈现了我的Drop dwon列表以及数据库中的日期.一切都很完美.但是...

I've read this post: ASP.NET MVC Razor pass model to layout it's more or less similar to my problem. In one of comments Mattias Jakobsson wrote that: "But a common solution is to use RenderAction to render parts that need their own data in the layout page". So ok I've created layout page with @Html.Action() that render my drop dwon list with a date from the db. Everything's perfect. But...

  1. 我有两个页面,例如:主页",关于"和布局页面上的下拉列表(ddl)
  2. 当我在首页"并且更改了ddl中的选择时,如何刷新主页"页面,而当我在关于"时刷新关于"页面.
  3. 如何通过页面存储选定的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);
    }
   .
   .
   }

关于, 马辛

推荐答案

好的,我已经设法解决了这个问题,我想知道您的观点和我的解决方案是否相符.

ok I've managed to solve this problem and I want to know your opinion abut my solution.

_Layout.cshtml的外观与第一篇文章相同,因此,below仅是该问题的最重要部分(布局的下拉列表)

_Layout.cshtml looks the same way like at first post, so belowe is only most important part for this question (drop down list at layout)

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

操作:VariantsDdl在MyBaseController上实现.此操作从会话中加载选定的变体ID,或者如果它为null,则从web.config中加载(在这种情况下,根据项目要求,必须在db中至少存在一个变体,并且必须在config中指定其变体):

Action: VariantsDdl is implemented at MyBaseController. This action loads selected variant id from session or if it's null then from web.config (in this situation it's project requirement that at least one variant must be present at db and its id must be specified in config):

    [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存储到会话中的部分视图和发布操作:

Partial view and post action that stores selected variant id to 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'); }
        });

    });
});

部分视图后操作'ChangeVariant',将选定的变体ID保存到会话:

Partial View post action 'ChangeVariant', saves selected variant id to session:

   [HttpPost]
    public ActionResult ChangeVariant(long id = 0)
    {
        System.Web.HttpContext.Current.Session["mySelectedVariant"] = id;

        return null;
    }

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

This is solution for my requirements: 1. DDL at layout 2. Refresh current page at DDL 'onchange' 3. Keep selected DDL value through pages

请问是否是合适的解决方案,或者我应该采取其他方式?

Please comment if it's appropriate solution or maybe should I go different way?

关于, 马辛

这篇关于布局页面上的下拉列表-MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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