Mar*_*cin 2 c# asp.net-mvc razor asp.net-mvc-4
我的问题:在布局页面上的下拉列表。
我读了这篇文章:ASP.NET MVC Razor通过模型来布局它或多或少与我的问题相似。Mattias Jakobsson在评论中写道:“但是,常见的解决方案是使用RenderAction在布局页面中呈现需要自己数据的零件”。好吧,我已经使用@ Html.Action()创建了布局页面,该页面呈现了带有数据库中日期的dwon列表。一切都很完美。但...
部分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()
.
.
Run Code Online (Sandbox Code Playgroud)
部分 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);
}
.
.
}
Run Code Online (Sandbox Code Playgroud)
此致Marcin
好的,我已经设法解决了这个问题,我想知道您的意见以及我的解决方案。
_Layout.cshtml的外观与第一篇文章相同,因此,below仅是此问题的最重要部分(布局的下拉列表)
<div style="float: right;">
@Html.Action("VariantsDdl", "MyBase")
</div>
Run Code Online (Sandbox Code Playgroud)
行动:VariantsDdl是在MyBaseController上实现的。此操作从会话中加载选定的变体ID,或者如果它为null,则从web.config中加载(在这种情况下,这是项目要求,必须在db中至少存在一个变体,并且必须在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);
}
Run Code Online (Sandbox Code Playgroud)
将所选变体ID存储到会话的部分视图和发布操作:
@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'); }
});
});
});
Run Code Online (Sandbox Code Playgroud)
部分查看后操作'ChangeVariant',将选定的变体ID保存到会话中:
[HttpPost]
public ActionResult ChangeVariant(long id = 0)
{
System.Web.HttpContext.Current.Session["mySelectedVariant"] = id;
return null;
}
Run Code Online (Sandbox Code Playgroud)
这是满足我要求的解决方案:1.布局中的DDL 2.在DDL“ onchange”处刷新当前页面3.保留选定的DDL值通过页面
请评论它是否合适,或者我应该采用其他方式?
此致Marcin
| 归档时间: |
|
| 查看次数: |
7714 次 |
| 最近记录: |