我可以通过 UIHint 控制参数发送 SelectList 吗?

发布于 2024-11-29 04:03:59 字数 431 浏览 4 评论 0原文

我可以通过数据注释发送 SelectList 吗?就像......

[UIHint("DropDownList", "", new SelectList(new[] {"one","two","three"}))]
public virtual int? OptionID { get; set; }

我不明白语法,但这似乎是可能的。如果是这样,我如何从编辑器模板访问它?

如果没有,我如何动态地将 SelectList 发送到 DropDownList 编辑器模板?我特别想避免为每个 SelectList 制作单独的模板 - 我有太多的模板。谢谢

编辑:我正在研究第二个选项(反射),因为我认为它可能比覆盖那个 15 音节怪物 DataAnnotationsModelMetadataProvider 更直接。

Can I send a SelectList through a Data Annotation? Something like...

[UIHint("DropDownList", "", new SelectList(new[] {"one","two","three"}))]
public virtual int? OptionID { get; set; }

I don't understand the syntax but this seems possible. If so, how do I access it from an editor template?

If not, how could I dynamically send a SelectList to a DropDownList Editor Template? I specifically would like to avoid making a separate template for every SelectList - I have too many of them. Thanks

EDIT: I'm working on the second option (Reflection) because I thought it might be more direct than overriding that 15-syllable monster, the DataAnnotationsModelMetadataProvider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2024-12-06 04:03:59

您可能想看看这篇博客文章:

http:// /mikevdm.com/BlogEntry/Key/Using-UIHint-With-ControlParameters-in-MVC

这是相关引用:

ControlParameters 的局限性在于它们只能接受(编译时)常量值,采用一种有点不寻常的语法,但它们确实允许简单的值(真/假、枚举值等),因此您的模板化助手可以表现根据参数略有不同。这使您可以将通常在各个模板(具有大量重复代码)中找到的相关功能组合到一个模板中。

在我的应用程序中,我的所有下拉列表都是可为空的布尔值(是、否、未设置)或枚举。我采取了为每个模板制作一个单独的模板的方法,但使用辅助方法使每个模板中的代码非常少。

例如,我有一个名为 Level 的模板(其中 Level 是一个枚举):

该代码只是一些使用加上...

@Html.DropDownListFor(model => model, Model.ToSelectList<Level>())

我将这些扩展方法用于 ToSelectList

public static SelectList ToSelectList<TEnum>(this TEnum? value) where TEnum : struct
{
    var items = GetSelectListItems<TEnum>().ToList();
    items.Insert(0, new SelectListItem { Value = "", Text = LabelIfNull });
    return new SelectList(items, "Value", "Text", value.ToString());
}

public static SelectList ToSelectList<TEnum>(this TEnum value)
{
    var items = GetSelectListItems<TEnum>();
    return new SelectList(items, "Value", "Text", value.ToString());
}

public static IEnumerable<SelectListItem> GetSelectListItems<TEnum>()
{
    var values = System.Enum.GetNames(typeof(TEnum));
    return values.Select(v => new SelectListItem { Value = v, Text = v.ToFriendlyName() });
}

public static SelectList ToSelectList(this bool isTrue)
{
    var items = new[]
    {
        new SelectListItem { Value = "true", Text = LabelIfTrue },
        new SelectListItem { Value = "false", Text = LabelIfFalse }
    };
    return new SelectList(items, "Value", "Text", isTrue.ToString());
}

public static SelectList ToSelectList(this bool? isTrue)
{
    var items = new[]
    {
        new SelectListItem { Value = string.Empty, Text = LabelIfNull },
        new SelectListItem { Value = "true", Text = LabelIfTrue },
        new SelectListItem { Value = "false", Text = LabelIfFalse }
    };
    return new SelectList(items, "Value", "Text", !isTrue.HasValue ? string.Empty : isTrue.Value.ToString());
}

也许您可以使用控制参数来简化为单个模板,但您肯定需要编写自己的 DataAnnotationsModelMetadataProvider,如博客文章中所示。

You might want to take a look at this blog post:

http://mikevdm.com/BlogEntry/Key/Using-UIHint-With-ControlParameters-in-MVC

Here's a relevant quote:

ControlParameters are limited in that they can only accept (compile-time) constant values, in a somewhat unusual syntax, but they do allow simple vales (true/false, enumerated values, etc.), so your templated helper can behave slightly differently based on parameters. This allows you to combine related functionality, usually found in individual templates (with lots of code duplication), into one single template.

In my app, all my drop down lists were either nullable bools (Yes, No, not set) or enums. I took the route of making a separate template for each one, but using helper methods to make the code within each template very minimal.

For example, I have a template called Level (where Level is an enum):

The code is just a couple usings plus....

@Html.DropDownListFor(model => model, Model.ToSelectList<Level>())

I use these extension methods for ToSelectList:

public static SelectList ToSelectList<TEnum>(this TEnum? value) where TEnum : struct
{
    var items = GetSelectListItems<TEnum>().ToList();
    items.Insert(0, new SelectListItem { Value = "", Text = LabelIfNull });
    return new SelectList(items, "Value", "Text", value.ToString());
}

public static SelectList ToSelectList<TEnum>(this TEnum value)
{
    var items = GetSelectListItems<TEnum>();
    return new SelectList(items, "Value", "Text", value.ToString());
}

public static IEnumerable<SelectListItem> GetSelectListItems<TEnum>()
{
    var values = System.Enum.GetNames(typeof(TEnum));
    return values.Select(v => new SelectListItem { Value = v, Text = v.ToFriendlyName() });
}

public static SelectList ToSelectList(this bool isTrue)
{
    var items = new[]
    {
        new SelectListItem { Value = "true", Text = LabelIfTrue },
        new SelectListItem { Value = "false", Text = LabelIfFalse }
    };
    return new SelectList(items, "Value", "Text", isTrue.ToString());
}

public static SelectList ToSelectList(this bool? isTrue)
{
    var items = new[]
    {
        new SelectListItem { Value = string.Empty, Text = LabelIfNull },
        new SelectListItem { Value = "true", Text = LabelIfTrue },
        new SelectListItem { Value = "false", Text = LabelIfFalse }
    };
    return new SelectList(items, "Value", "Text", !isTrue.HasValue ? string.Empty : isTrue.Value.ToString());
}

Perhaps you could use control parameters to get down to a single template, but you'll definitely need to write your own DataAnnotationsModelMetadataProvider, as indicated in the blog post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文