在我的模型中,我有一个实体
public class Carrier
{
public Guid CarrierId { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我也有一个ViewModel
public class CarrierIndexViewModel
{
public IEnumerable<Carrier> Carriers { get; set; }
public PagingInfo PagingInfo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个强类型(CarrierIndexViewModel)索引视图,它假设使用PagingInfo显示Carrier表.
我正在尝试使用Html帮助器在我的表头中显示Carrier.Name当我使用IEnumerable作为此视图的模型时,我有
@Html.DisplayFor(model => model.Name)
如何使用我的新CarrierIndexViewModel显示Carrier.Name的标题?
我正在开发一个MVC 5应用程序.我想在我的控制器方法中的 [Display(Name ="")]属性中获取任何类的任何属性的值.
我的模型如下:
public partial class ABC
{
[Required]
[Display(Name = "Transaction No")]
public string S1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我看过这个问题的答案,但这是一个有点冗长的程序.我正在寻找随时可用和内置的东西.
所以,我试过这个:
MemberInfo property = typeof(ABC).GetProperty(s); // s is a string type which has the property name ... in this case it is S1
var dd = property.CustomAttributes.Select(x => x.NamedArguments.Select(y => y.TypedValue.Value)).OfType<System.ComponentModel.DataAnnotations.DisplayAttribute>();
Run Code Online (Sandbox Code Playgroud)
但我有两个问题,首先我没有得到价值,即"交易否".其次,尽管我已经提到.OfType <>我仍然得到所有属性,即[Display(Name ="")]和[Required].
但幸运的是我获得了"交易否"的价值
property >> CustomAttribute >> [1] >> NamedArguments >> [0] >> TypedValue >> Value ="Transaction No"
由于 …
我发现@RichTebb的代码很棒,它返回Model属性DisplayName.
但是如何遍历所有模型显示(Name =)属性值呢?
谢谢你的任何线索!
@RichTebb代码
public static class HelperReflectionExtensions
{
public static string GetPropertyDisplayString<T>(Expression<Func<T, object>> propertyExpression)
{
var memberInfo = GetPropertyInformation(propertyExpression.Body);
if (memberInfo == null)
{
throw new ArgumentException(
"No property reference expression was found.",
"propertyExpression");
}
var displayAttribute = memberInfo.GetAttribute<DisplayAttribute>(false);
if (displayAttribute != null)
{
return displayAttribute.Name;
}
// ReSharper disable RedundantIfElseBlock
else
// ReSharper restore RedundantIfElseBlock
{
var displayNameAttribute = memberInfo.GetAttribute<DisplayNameAttribute>(false);
if (displayNameAttribute != null)
{
return displayNameAttribute.DisplayName;
}
// ReSharper disable RedundantIfElseBlock
else
// ReSharper …Run Code Online (Sandbox Code Playgroud) 我知道有一个DisplayName和Display类似的模型属性:
public class MyModel
{
[Display(Name = "Licence")]
public string Licence { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但有一个Display对整个模型?可能是这样的:
[Display(Name = "My beautiful model")]
public class MyModel
{
...
Run Code Online (Sandbox Code Playgroud)
如果是这样,如何从HTML访问它?
我正在使用带有EntityFramework 6 DataAnnotations的asp.net MVC 5。我想知道是否有一种方法来获取所有DisplayName对象并将它们保存在变量中到Controller类中。
例如,考虑以下类:
public class Class1
{
[DisplayName("The ID number")]
public int Id { get; set; }
[DisplayName("My Value")]
public int Value { get; set; }
[DisplayName("Label name to display")]
public string Label { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何获取DisplayName所有属性的值?例如,如何创建一个返回a的函数,该函数Dictionary< string,string >的键具有属性名称和值为的键DisplayName,如下所示:
{ "Id": "The ID name", "Value": "My Value", "Label": "Label name to display"}.
Run Code Online (Sandbox Code Playgroud)
我已经看到了这个主题stackoverflow-获取DisplayName属性的值,但是我不知道扩展此代码的方法。