对于DropDownList的编辑模板ASP.NET MVC内置支持 [英] ASP.NET MVC built-in support for DropDownList editor template

查看:101
本文介绍了对于DropDownList的编辑模板ASP.NET MVC内置支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法说我的视图模型属性应呈现为的DropDownList (这样我可以指定的DropDownList 项目)?

Is there any way to say my view model property should be rendered as DropDownList (so that I can specify DropDownList items)?

我已经找到了很多定制实现的,但我想应该有实现这样一个基本的东西一个内置的方式。

I have found a lot of custom implementations but I guess there should be a built-in way to implement such a basic thing.

更新。我对 Html.EditorForModel 渲染方法我的模型,我不希望使用方法,如 Html.DropDownListFor

Update. I am rendering my model by Html.EditorForModel method, I don't want to use methods like Html.DropDownListFor

推荐答案

有没有内置的模板,呈现一个下拉列表中,除了可空<布尔> 键入这使得一个未设置下拉菜单,但我认为这不是你问什么。

There's no built-in template which renders a dropdown list, except for the Nullable<bool> type which renders a Not Set, Yes, No dropdown but I assume that's not what you are asking about.

因此​​,让我们建立一个。与往常一样,我们首先来定义,这将重新present视图模型包含2个属性(一个用于选定值和一个可用的值)下拉:

So let's build one. As always we start by defining the view model that will represent a dropdown containing 2 properties (one for the selected value and one for the available values):

public class ItemViewModel
{
    public string SelectedId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

那么我们就可以与该物业的标准视图模型:

then we could have a standard view model with this property:

public class MyViewModel
{
    public ItemViewModel Item { get; set; }
}

然后控制器,将填补视图模型:

then a controller that will fill the view model:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Item = new ItemViewModel
            {
                SelectedId = "2",
                Items = new[]
                {
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" },
                    new SelectListItem { Value = "3", Text = "item 3" },
                }
            }
        };
        return View(model);
    }
}

和相应的视图(〜/查看/主页/ Index.cshtml

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.EditorForModel()
}

现在,所有剩下的是定义为 DropDownViewModel 键入(自定义编辑器模板〜/查看/共享/ EditorTemplates / DropDownViewModel.cshtml

Now all that's left is to define a custom editor template for the DropDownViewModel type (~/Views/Shared/EditorTemplates/DropDownViewModel.cshtml):

@model DropDownViewModel
@Html.DropDownListFor(
    x => x.SelectedId, 
    new SelectList(Model.Items, "Value", "Text", Model.SelectedId)
)

和覆盖Object类型的默认模板,以允许的深潜的布拉德·威尔逊解释<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html\"><$c$c>his博客文章 。否则,默认情况下ASP.NET MVC不会递归到你的模板复杂的亚型。所以,我们覆盖〜/查看/共享/ EditorTemplates / Object.cshtml

and override the default template for the Object type in order to allow Deep Dive as Brad Wilson explains in his blog post. Otherwise by default ASP.NET MVC won't recurse into complex subtypes for your templates. So we override ~/Views/Shared/EditorTemplates/Object.cshtml:

@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) 
{
    if (prop.HideSurroundingHtml) 
    {
        @Html.Editor(prop.PropertyName)
    } 
    else 
    { 
        <div class="editor-label">
            @(prop.IsRequired ? "*" : "")
            @Html.Label(prop.PropertyName)
        </div>
        <div class="editor-field">
            @Html.Editor(prop.PropertyName)
            @Html.ValidationMessage(prop.PropertyName, "*")
        </div>
    }
}

这篇关于对于DropDownList的编辑模板ASP.NET MVC内置支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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