gpt4 book ai didi

c# - 不要在 View 中显示属性

转载 作者:行者123 更新时间:2023-11-30 22:57:16 25 4
gpt4 key购买 nike

ServiceStack 中是否有 MVC [HiddenInput(DisplayValue = false)] 的等价物?

我不想在 View 中显示特定的模型属性。我已经创建了自己的 HTML 帮助程序扩展方法来显示所有基于 System.ComponentModel.DisplayNameAttribute 的属性值,并希望使用属性来停止显示它。

这是 View :

@inherits ViewPage<GetCustomersubscriptionsResponse>

@{
ViewBag.Title = string.Format("History > subscriptions > Customer {0}", Model.CustomerId);
Layout = "CustomerOfficeUIFabric";
}
<div class="tableContainer">
@if (Model.subscriptions != null && Model.subscriptions.Count > 0)
{
<table class="ms-Table" style="max-width:800px;">
<thead>
<tr>
@{
Type subscriptionType = Model.subscriptions.GetType().GetGenericArguments()[0];
}
@Html.GenerateHeadings(subscriptionType)
</tr>
</thead>
<tbody>
@foreach (var subscription in Model.subscriptions)
{
@Html.GenerateRow(subscription)
}
</tbody>
</table>
}
else
{
<div class="notFound ms-font-m-plus">No records found</div>
}
</div>

下面是扩展方法:

public static class HtmlHelperExtensions
{
public static MvcHtmlString GenerateRow(this HtmlHelper htmlHelper, object Subscription)
{
var sb = new StringBuilder();
sb.Append("<tr>");
Type SubscriptionType = Subscription.GetType();
foreach (PropertyInfo propertyInfo in SubscriptionType.GetProperties())
{
object propertyValue = propertyInfo.GetValue(Subscription, null);
sb.Append($"<td>{propertyValue}</td>");
}
sb.Append("</tr>");

return new MvcHtmlString(sb.ToString());
}

public static MvcHtmlString GenerateHeadings(this HtmlHelper htmlHelper, Type modelType)
{
var sb = new StringBuilder();

List<string> displayNames = GetDisplayNames(modelType);

foreach (var displayName in displayNames)
{
sb.Append($"<th>{displayName}</th>");
}

return new MvcHtmlString(sb.ToString());
}

private static List<string> GetDisplayNames(Type modelType)
{
List<string> displayNames = new List<string>();

PropertyInfo[] props = modelType.GetProperties();
foreach (PropertyInfo prop in props)
{
string displayNameAttributeValue = GetDisplayNameAttributeValue(prop);
string heading = !string.IsNullOrWhiteSpace(displayNameAttributeValue) ? displayNameAttributeValue : prop.Name;
displayNames.Add(heading);
}

return displayNames;
}

private static string GetDisplayNameAttributeValue(PropertyInfo prop)
{
object[] attributes = prop.GetCustomAttributes(false);
if (attributes.Any())
{
var displayNameAttributes = attributes.Where(x => x is DisplayNameAttribute);
if (displayNameAttributes.Any())
{
var displayNameAttribute = displayNameAttributes.First() as DisplayNameAttribute;
return displayNameAttribute.DisplayName;
}
}
return null;
}
}

最佳答案

此逻辑要么需要在您用于在 View 中呈现 HTML 表格的库/功能中,例如:

foreach (var propertyInfo in SubscriptionType.GetProperties())
{
if (propertyInfo.HasAttribute<HiddenInputAttribute>()) continue;
//...
}

还有一些Auto Mapping Utils您可以使用它从 View 模型中删除不需要的属性。

public class ViewModel
{
public string Public { get; set; }

[HiddenInput]
public string Private { get; set; }
}

您可以创建一个不包含 [HiddenInput] 属性的新 View 模型:

viewModel = new ViewModel().PopulateFromPropertiesWithoutAttribute(
viewModel, typeof(HiddenInputAttribute));

或者您可以使用 ToObjectDictionary在非结构化字典中操作模型属性,例如:

var map = viewModel.ToObjectDictionary();
viewModel.GetType().GetProperties()
.Where(x => x.HasAttribute<HiddenInputAttribute>())
.Each(x => map.Remove(x.Name)); //remove all props with [HiddenInput]

viewModel = map.FromObjectDictionary<ViewModel>(); //new viewModel without removed props

关于c# - 不要在 View 中显示属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com