简体   繁体   English

检查视图模型属性是否为字符串

[英]Check if a view model property is a string

I have the following foreach in a view, I would like it to iterate over properties in my view model and display a text box for every property that is a string. 我在视图中具有以下foreach,我希望它遍历视图模型中的属性,并为每个字符串属性显示一个文本框。

foreach (var property in ViewData.ModelMetadata.Properties)
        {
            if(property.GetType() == "string")
            {
                <div class="watermark">
                    <label>Friend's email address</label>
                    <br>
                    @Html.Editor(property.PropertyName)
                </div>
            }
        }

Ideally property.GetType() == "string" would return a true or false, but this is not the case because .GetType is always returning System.Type (I believe). 理想情况下,property.GetType()==“ string”将返回true或false,但这不是事实,因为.GetType始终返回System.Type(我相信)。

尝试这个

if(property.GetType() == typeof(string))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM