MVC 3 布局页面、Razor 模板和下拉列表 [英] MVC 3 Layout Page, Razor Template, and DropdownList

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

问题描述

我想在我网站的所有页面中包含年份的下拉列表.我认为放置此逻辑的好地方是在布局页面 (_layout.cshtml) 中.如果用户更改年份,我也想更改我的年份会话 (ModelBinder) 以进行更改.使用 ASP.NET Web 表单很容易做到这一点,但在 MVC 中似乎几乎不可能做到.我尝试了部分视图,但没有运气.有人有什么想法吗?

解决方案

像往常一样,你可以从定义一个视图模型开始:

公共类 YearsViewModel{公共字符串年{得到;放;}公共 IEnumerable年{得到{返回新的选择列表(Enumerable.Range(1900, 112).OrderByDescending(年 => 年).Select(year => new SelectListItem{值 = year.ToString(),文本 = 年.ToString()}), "值", "文本");}}}

然后是控制器:

public class YearsController : 控制器{公共 ActionResult 索引(){返回视图(新的年视图模型());}[HttpPost]公共 ActionResult 索引(整数年){//TODO: 对选定的年份做一些事情返回新的 EmptyResult();}}

以及索引操作的相应视图:

@model SomeAppName.Models.YearsViewModel@{布局 = 空;}@Html.DropDownListFor(x => x.Year, Model.Years)

最后在你的 _Layout.cshtml 中你可以使用这个控制器:

@Html.Action("index", "years")

并附上相应的脚本,该脚本会在值更改时发送 AJAX 请求:

$(function () {$('#selectyear select').change(function () {$.post('@Url.Action("index", "years")', { year: $(this).val() }, function (result) {});});});

I want to include a drop down list of years across all the pages in my website. I assumed a good place to put this logic was in the layout page (_layout.cshtml). If a user changes the year I want to change my year session (ModelBinder) to change as well. This was so easy to do with ASP.NET web forms, but seems near impossible to do in MVC. I tried a partial view with no luck. Anybody have any ideas?

解决方案

As usual you could start by defining a view model:

public class YearsViewModel
{
    public string Year { get; set; }
    public IEnumerable<SelectListItem> Years
    {
        get
        {
            return new SelectList(
                Enumerable.Range(1900, 112)
                .OrderByDescending(year => year)
                .Select(year => new SelectListItem
                {
                    Value = year.ToString(),
                    Text = year.ToString()
                }
            ), "Value", "Text");
        }
    }
}

Then a controller:

public class YearsController : Controller
{
    public ActionResult Index()
    {
        return View(new YearsViewModel());
    }

    [HttpPost]
    public ActionResult Index(int year)
    {
        // TODO: do something with the selected year
        return new EmptyResult();
    }
}

and a corresponding view for the index action:

@model SomeAppName.Models.YearsViewModel
@{
    Layout = null;
}
@Html.DropDownListFor(x => x.Year, Model.Years)

And finally inside your _Layout.cshtml you could use this controller:

<div id="selectyear">@Html.Action("index", "years")</div>

and attach a corresponding script which would send an AJAX request when the value changes:

$(function () {
    $('#selectyear select').change(function () {
        $.post('@Url.Action("index", "years")', { year: $(this).val() }, function (result) {

        });
    });
});

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

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