c# - 抽象属性的 MVC 属性

标签 c# .net asp.net-mvc

我有一个抽象模型

public abstract class Treasure {
    public abstract int Value { get; }
    public abstract string Label { get; }
}

和一个实现

public class Coins : Treasure {
    [DisplayName("Coin Value")]
    public override int Value {
        get { ... }
    }

    [DisplayName("Coins")]
    public override string Label {
        get { ... }
    }

当我在其上使用 Html.LabelFor 时,我的硬币对象在我的 View 中没有显示“硬币”作为其标签,它显示“标签”。如果我将 DisplayName 属性移到 Treasure 中,它就可以工作......但我需要能够更改 Treasure 类的不同实现的标签。这可能吗?

最佳答案

如果 View 中的模型是硬币,我就能让它正常工作。但是,如果模型是 Treasure,则失败。为什么?因为当 Html Helper 呈现 Html 时,它只查看 View 中指定的模型类型,而不是实际对象的对象类型。当它去获取属性时,它只会获取 Treasure 而不是 Coins 的属性。我认为您必须为此编写自己的 Html 帮助器。

internal static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, IDictionary<string, object> htmlAttributes, ModelMetadataProvider metadataProvider)
{
  return LabelExtensions.LabelHelper((HtmlHelper) html, ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData, metadataProvider), ExpressionHelper.GetExpressionText((LambdaExpression) expression), labelText, htmlAttributes);
}

在幕后,MVC 使用 ModelMetadata.FromLambdaExpression<TModel, TValue>找到“DisplayName”,当它找不到传入的类型时...它返回 PropertyName .

internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionary<string, object> htmlAttributes = null)
{
  string str = labelText;
  if (str == null)
  {
    string displayName = metadata.DisplayName;
    if (displayName == null)
    {
      string propertyName = metadata.PropertyName;
      if (propertyName == null)
        str = Enumerable.Last<string>((IEnumerable<string>) htmlFieldName.Split(new char[1]
        {
          '.'
        }));
      else
        str = propertyName;
    }
    else
      str = displayName;
  }
  string innerText = str;
  if (string.IsNullOrEmpty(innerText))
    return MvcHtmlString.Empty;
  TagBuilder tagBuilder1 = new TagBuilder("label");
  tagBuilder1.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
  tagBuilder1.SetInnerText(innerText);
  TagBuilder tagBuilder2 = tagBuilder1;
  bool flag = true;
  IDictionary<string, object> attributes = htmlAttributes;
  int num = flag ? 1 : 0;
  tagBuilder2.MergeAttributes<string, object>(attributes, num != 0);
  return TagBuilderExtensions.ToMvcHtmlString(tagBuilder1, TagRenderMode.Normal);
}

关于c# - 抽象属性的 MVC 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19104359/

相关文章:

c# - 用户登录成功后未通过身份验证

c# - 在同一项目(SQL Server 和 MySql)中使用具有多个数据库和提供程序的 Entity Framework

c# - 批量注册 IEntityTypeConfiguration<> Entity Framework 核心

c# - 在 WPF 中强制 App 继承自 System.Windows.Application 的类

.net - WCF - 赢取应用程序 “The maximum string content length quota (8192) has been exceeded while reading XML data.”

c# - 我在 JIT/CLR 中发现了一个错误 - 现在我该如何调试或重现它?

c# - 如果我们有单一操作,使用异步操作的目的是什么?

c# - 向 ASP.NET MVC4 项目添加类

c# - 替换 ObservableCollection 的项目时如何收到通知

c# - 收集没有重复但有顺序,快速包含检查和添加/删除