gpt4 book ai didi

c# - 如何获取 TagHelper.Process 中的属性属性类型?

转载 作者:行者123 更新时间:2023-11-30 18:14:48 25 4
gpt4 key购买 nike

我写了一个标签助手,我需要获取模型属性的类型,因为我想基于它创建一个 html 标签实例。

如果它的类型是Boolean,我想创建Checkbox 的实例等等。

[HtmlTargetElement("edit")]
public class EditTagHelper : TagHelper
{
[HtmlAttributeName("asp-for")]
public ModelExpression aspFor { get; set; }

[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }

protected IHtmlGenerator _generator { get; set; }

public EditTagHelper(IHtmlGenerator generator)
{
_generator = generator;
}

public override void Process(TagHelperContext context, TagHelperOutput output)
{
TagBuilder instance = new TagBuilder("div");
var propName = aspFor.ModelExplorer.Model.ToString();

var modelExProp = aspFor.ModelExplorer.Container.Properties.Single(x => x.Metadata.PropertyName.Equals(propName));
var propValue = modelExProp.Model;
var propEditFormatString = modelExProp.Metadata.EditFormatString;

var label = _generator.GenerateLabel(ViewContext, aspFor.ModelExplorer,
propName, propName, new { @class = "col-md-2 control-label", @type = "email" });

var typeOfProperty = // HOW CAN I GET TYPE OF PROPERTY ???;
if (typeOfProperty == typeof(Boolean))
{
bool isChecked = propValue.ToString().ToLower() == "true";
instance = _generator.GenerateCheckBox(ViewContext, aspFor.ModelExplorer, propName, isChecked, new { @class = "form-control" });
}
}
}

Updated:

用户控件:

   [HttpGet]
public IActionResult Edit(string id)
{
var propertyNames = new List<string>();
var userProperties = typeof(User).GetProperties();

foreach (PropertyInfo prop in userProperties)
{
Type type = prop.PropertyType;
if (!(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ICollection<>)))
{
string attrName = string.Empty;
var attribute = (DisplayNameAttribute)prop.GetCustomAttribute(typeof(DisplayNameAttribute), true);
if (attribute != null)
{
attrName = attribute.DisplayName;
}
else
{
attrName = prop.Name;
}

propertyNames.Add(attrName);
}
}
ViewData["PropertyList"] = propertyNames;

try
{
if (string.IsNullOrEmpty(id))
{
return RedirectToAction("Index", "Users");
}
User user = _userManager.Users.FirstOrDefault(u => u.Id == int.Parse(id));
return View(user);
}
catch (Exception)
{
throw;
}
}

Edit.cshtml:

@using System.ComponentModel
@using System.Reflection
@using Jahan.Beta.Web.App.Models.Identity
@using Jahan.Beta.Web.App.Infrastructure
@model Jahan.Beta.Web.App.Models.Identity.User

<div class="row">
@using (Html.BeginForm())
{
var propertyNames = (List<string>)ViewData["PropertyList"];

foreach (string item in propertyNames)
{
<edit asp-for="@item"></edit>
}
<input type="submit" value="Submit" />
}
</div>

(如果您通过ViewData 传递属性列表(list of PropertyInfo)进行查看,则您无法访问EditTagHelper.cs 中的模型值,或者至少我做不到!因此,我通过 ViewData (ViewData["PropertyList"]))

传递了属性名称

最佳答案

ModelExplorerModelType其中包含模型的类型:

var typeOfProperty = modelExProp.ModelType;

您还可以通过调用 Object.GetType() 直接从属性值中获取属性类型:

var typeOfProperty = propValue?.GetType();

关于c# - 如何获取 TagHelper.Process 中的属性属性类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49701539/

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