servicestack,C#,Razor,servicestack" /> servicestack,C#,Razor,servicestack" />

C# 不在视图中显示属性

C# 不在视图中显示属性,c#,razor,servicestack,C#,Razor,servicestack,ServiceStack中是否有MVC[HiddenInput(DisplayValue=false)]的等价物 我不希望在视图中显示特定的模型特性。 我已经创建了自己的HTML helper扩展方法,以显示基于System.ComponentModel.DisplayNameAttribute的所有属性值,并希望使用一个属性来停止显示它 以下是视图: @inherits ViewPage<GetCustomersubscriptionsResponse> @{ ViewB

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

我不希望在视图中显示特定的模型特性。 我已经创建了自己的HTML helper扩展方法,以显示基于
System.ComponentModel.DisplayNameAttribute的所有属性值,并希望使用一个属性来停止显示它

以下是视图:

@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>
@继承ViewPage
@{
ViewBag.Title=string.Format(“历史>订阅>客户{0}”,Model.CustomerId);
Layout=“CustomerOfficeUIFabric”;
}
@if(Model.subscriptions!=null&&Model.subscriptions.Count>0)
{
@{
类型subscriptionType=Model.subscriptions.GetType().GetGenericArguments()[0];
}
@Html.GenerateHeadings(subscriptionType)
@foreach(Model.subscriptions中的var订阅)
{
@Html.GenerateRow(订阅)
}
}
其他的
{
没有发现任何记录
}
以下是扩展方法:

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;
    }
}
公共静态类HtmlHelperExtensions
{
公共静态MvcHtmlString GeneratorOW(此HtmlHelper HtmlHelper,对象订阅)
{
var sb=新的StringBuilder();
某人加上(“”);
类型SubscriptionType=Subscription.GetType();
foreach(SubscriptionType.GetProperties()中的PropertyInfo PropertyInfo)
{
object propertyValue=propertyInfo.GetValue(订阅,null);
sb.追加($“{propertyValue}”);
}
某人加上(“”);
返回新的MvcHtmlString(sb.ToString());
}
公共静态MvcHtmlString生成标题(此HtmlHelper HtmlHelper,类型modelType)
{
var sb=新的StringBuilder();
List displayNames=GetDisplayNames(modelType);
foreach(displayNames中的变量displayName)
{
sb.追加($“{displayName}”);
}
返回新的MvcHtmlString(sb.ToString());
}
私有静态列表GetDisplayNames(类型modelType)
{
List displayNames=新列表();
PropertyInfo[]props=modelType.GetProperties();
foreach(PropertyInfo props in props)
{                
字符串displayNameAttributeValue=GetDisplayNameAttributeValue(prop);
字符串标题=!string.IsNullOrWhiteSpace(displayNameAttributeValue)?displayNameAttributeValue:prop.Name;
displayNames.Add(标题);
}
返回显示名;
}
私有静态字符串GetDisplayNameAttributeValue(PropertyInfo prop)
{
object[]attributes=prop.GetCustomAttributes(false);
if(attributes.Any())
{
var displayNameAttributes=attributes,其中(x=>x是DisplayNameAttribute);
if(displayNameAttributes.Any())
{
var displayNameAttribute=displayNameAttributes.First()作为displayNameAttribute;
返回displayNameAttribute.DisplayName;
}
}
返回null;
}
}

此逻辑可能需要位于用于在视图中呈现HTML表的库/功能中,例如:

foreach (var propertyInfo in SubscriptionType.GetProperties())
{
    if (propertyInfo.HasAttribute<HiddenInputAttribute>()) continue;
    //...
}
您可以创建一个新的视图模型,而不使用包含
[HiddenInput]
属性的属性,该属性具有:

viewModel = new ViewModel().PopulateFromPropertiesWithoutAttribute(
    viewModel, typeof(HiddenInputAttribute));
或者,您可以使用来操作非结构化字典中的模型属性,例如:

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
var-map=viewModel.ToObjectDictionary();
viewModel.GetType().GetProperties()
.Where(x=>x.HasAttribute())
.Each(x=>map.Remove(x.Name))//使用[HiddenInput]移除所有道具
viewModel=map.FromObjectDictionary()//没有移除道具的新viewModel

你指的是什么视图?@mythz我已经更新了这个问题。ServiceStack没有引用这些属性中的任何一个,但我仍然不明白你用什么来渲染视图模型?支持将取决于您用于呈现HTML表的任何内容。@mythz可能我没有正确理解,但基本上我有一个创建模型的端点,该模型将传递给视图。如果这还不能回答您的问题,您能否提供一些人们可以用来渲染视图模型的示例,以帮助我理解您的问题。您使用什么来渲染视图模型?
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