How to use asp-for TagHelper combined with Reflection

别等时光非礼了梦想. 提交于 2021-02-08 05:14:27

问题


I have a class Foo which contains a lot of properties, I'd like to iterate through all of them into a form. The website is built using ASP.NET Core 2.2 and Razor.

How can I build a MemberExpression that asp-for TagHelper seems to expect using Reflection? I've read a couple of post about this on the Forum but none of the answer fits my need.

The following snippet doesn't work.

@model Foo;
<h1>Foo Reflective filling example</h1>

    @foreach(var property in typeof(Foo).GetProperties()) {
        <p>
            <div class="row">
                <div class="col-md-3">
                    <label asp-for="@property.Name"></label> @*doesn't work!'*@
                </div>
                <div class="col-md-3">
                    <input asp-for="@property.Name" class="form-control" id="property.Name"/>
                    <span asp-validation-for="@property.Name" class="text-danger"></span>
                </div>
            </div>
        </p>
    }

Thanks for your help


回答1:


The InputTagHelper helps us to write code in a style of declarative programming. When you find it difficult to render different fields dynamically by reflection, feel free to use the @Html.Xyz equivalent in a programmatic way.

Your code can be rewritten as below:

@foreach(var property in typeof(Foo).GetProperties()) {
    <p>
        <div class="row" >
            <div class="col-md-3">
                @Html.Label(@property.Name)
            </div>
            <div class="col-md-3">
                @Html.Editor(@property.Name,  new { htmlAttributes = new{ @class="form-control" } })
                @Html.ValidationMessage(@property.Name, new { htmlAttributes = new { @class="text-danger"} })
            </div>
        </div>
    </p>
}

Demo :

I create a custom Foo DTO as below :

public class Foo{
    public int Id {get;set;}
    public string Name {get;set;}
    public string Address {get;set;}
    public DateTime UpdatedAt{get;set;}
}

And the rendered form is :




回答2:


Right click to solution --> Add --> New Folder(Models)

Right click to Models folder --> Add --> Class(Foo)

Foo class:

public class Foo
{
    public int A { get; set; }
    public string B { get; set; }
}

Index.cshtml file:

@foreach (var property in typeof(YourProjectName.Models.Foo).GetProperties())
{
    <p>
        <div class="row">
            <div class="col-md-3">
                <label asp-for="@property.Name"></label>
            </div>
            <div class="col-md-3">
                <input asp-for="@property.Name" class="form-control" id="property.Name" />
                <span asp-validation-for="@property.Name" class="text-danger"></span>
            </div>
        </div>
    </p>
}

Result:

<body>
    <p>
        <div class="row">
            <div class="col-md-3">
                <label asp-for="A"></label> 
            </div>
            <div class="col-md-3">
                <input class="form-control" id="property.Name" asp-for="A">
                <span class="text-danger" asp-validation-for="A"></span>
            </div>
        </div>
    <p></p>
    <p>
        <div class="row">
            <div class="col-md-3">
                <label asp-for="B"></label> 
            </div>
            <div class="col-md-3">
                <input class="form-control" id="property.Name" asp-for="B">
                <span class="text-danger" asp-validation-for="B"></span>
            </div>
        </div>
    <p></p>
</body>


来源:https://stackoverflow.com/questions/58112633/how-to-use-asp-for-taghelper-combined-with-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!