在ASP.Net Core中进行模板 [英] Templating in ASP.Net Core

查看:165
本文介绍了在ASP.Net Core中进行模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 asp.net核心中创建子"模板时遇到问题. 这个想法是在模板文件中定义一个完整的html-bootstrap-block,然后在另一个父/主cshtml文件中调用它.

I have a problem creating a "sub"-template in asp.net core. The idea is to define a complete html-bootstrap-block in a template file and call it in another parent/master cshtml file.

ViewModel Person(填充在控制器中)

ViewModel Person (filled in the controller)

namespace RazorTemplating.Models.HomeViewModels
{
    public class Person
    {
        [Required]
        [MinLength(2)]
        public string Firstname { get; set; }

        [Required]
        [MinLength(2)]
        public string Lastname { get; set; }

        [MaxLength(3)]
        public int Age { get; set; }

        public Group MainGroup { get; set; }
    }
}

控制器调用的主视图(Index.cshtml)

The master view called by the controller (Index.cshtml)

编辑:输入模板标签引用了自定义标签帮助程序.

Edit: The input-template-tags reference a custom tag helper.

@model RazorTemplating.Models.HomeViewModels.Person

<div class="row">
    <input-template for="Firstname" />
</div>
<div class="row">
    <input-template for="Lastname" />
</div>
<div class="row">
    <input-template for="Age" />
</div>
<div class="row">
    <input-template for="MainGroup" />
</div>

Inputbox引导程序模板文件(InputBootstrapTemplate.cshtml)

Inputbox Bootstrap template file (InputBootstrapTemplate.cshtml)

@model dynamic

<div class="form-group">
    <label asp-for="@Model" class="col-md-2 control-label"></label>
    <div class="col-md-10">
        <input asp-for="@Model" class="form-control" />
        <span asp-validation-for="@Model" class="text-danger"></span>
    </div>
</div>

我如何通过 input-template 标签助手来填充模板文件的动态模型?

How i can fill the dynamic model for template file, from the input-template-tag helper?

在ASP.NET Core中进行模板制作时,已有一些预先更改. https://github.com. com/aspnet/Razor/issues/788

Something is changing in advance for templating in ASP.NET Core https://github.com/aspnet/Razor/issues/788

推荐答案

据我了解,您希望呈现类似自定义控件的内容.例如,渲染控件带有一些包装的html,标签,并以一行输入.正确的?在这种情况下,标记助手非常适合您的需求好.

As I understand you want to render something like custom controls. For example, render control with some wrapping html, label, input by one line. Right? In this case tag helpers fit your need very good.

您还可以查看新功能-查看组件.就像小型(一个动作)控制器+视图对.最好渲染新闻,最新添加的评论等区块.

Also you could take a look at new feature - view components. It is like small (one action) controller + view pair. It is good to render blocks, like news, last added comments etc.

随时询问详情.

这篇关于在ASP.Net Core中进行模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆