2

我正在尝试在带有 MVC 4 的 Visual Studio 2015 社区上使用自定义脚手架。

我的模型有一个自定义类型,一个由我创建的类。

样本:

public class Teste
{
    public DateTime data { get; set; }
    public string texto { get; set; }
    [DataType(DataType.Text)]
    public DateRange dateRange { get; set; }
}

我的脚手架 create.cs.t4 模板是:

<#@ template language="C#" HostSpecific="True" debug="true" #>
<#@ output extension=".cshtml" #>
<#@ import namespace="System.Diagnostics" #>
<#@ include file="Imports.include.t4" #>
@model <#= ViewDataTypeName #>
<#        try{  #>
<#
// "form-control" attribute is only supported for all EditorFor() in System.Web.Mvc 5.1.0.0 or later versions, except for checkbox, which uses a div in Bootstrap
string boolType = "System.Boolean";
Version requiredMvcVersion = new Version("5.1.0.0");
bool isControlHtmlAttributesSupported = MvcVersion >= requiredMvcVersion;
// The following chained if-statement outputs the file header code and markup for a partial view, a view using a layout page, or a regular view.
if(IsPartialView) 
{
#>

<#
} 
else if(IsLayoutPageSelected) 
{
#>

@{
    ViewBag.Title = "<#= ViewName#>";
<#
if (!String.IsNullOrEmpty(LayoutPageFile)) 
{
#>
    Layout = "<#= LayoutPageFile#>";
<#
}
#>
}
<#
} 
else {
#>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title><#= ViewName #></title>

<!-- Core CSS - Include with every page -->
<link href="@(ViewBag.URLAssets)css/bootstrap.min.css" rel="stylesheet">
<link href="@(ViewBag.URLAssets)font-awesome/css/font-awesome.css" rel="stylesheet">

<!-- Page-Level Plugin CSS - Forms -->

<!-- SB Admin CSS - Include with every page -->
<link href="@(ViewBag.URLAssets)css/sb-admin.css" rel="stylesheet">
</head>
<body>
<#
    PushIndent("    ");
}
#>
<#
if (ReferenceScriptLibraries) 
{
    if (!IsLayoutPageSelected && IsBundleConfigPresent) 
    {
#>
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryval")

<#
    }
#>
<#
    else if (!IsLayoutPageSelected) 
    {
#>
<script src="@(ViewBag.URLAssets)js/Scripts/jquery.min.js" type="text/javascript"></script>
<!-- SB Admin Scripts - Include with every page -->
<script src="@(ViewBag.URLAssets)js/Scripts/js/sb-admin.js"></script>
<#
    }
#>
@section scripts{
    <script src="@(ViewBag.URLAssets)js/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="@(ViewBag.URLAssets)js/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
    <script src="@(ViewBag.URLAssets)js/Scripts/bootstrap-validation.js" type="text/javascript"></script>
    <script src="@(ViewBag.URLAssets)js/Scripts/jquery.mask.min.js" type="text/javascript"></script>
}
<#
}
if (!IsLayoutPageSelected) 
{
#>
<div id="wrapper">
@Html.ViewContext.Writer.Write(Html.Partial("BarraSuperior"))
@Html.ViewContext.Writer.Write(Html.Partial("Menu"))
<#
}
#>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Insira os dados
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<#
Debugger.Launch();
foreach (PropertyMetadata property in ModelMetadata.Properties)
{
    if (property.Scaffold && !property.IsAutoGenerated && !property.IsReadOnly && !property.IsAssociation) 
    {
        // If the property is a primary key and Guid, then the Guid is generated in the controller. Hence, this propery is not displayed on the view.
        if (property.IsPrimaryKey && IsPropertyGuid(property)) {
            continue;
        }

#>
        <div class="form-group">
<#
        if (property.IsForeignKey) 
        {
#>
            @Html.LabelFor(model => model.<#= property.PropertyName #>, "<#= GetAssociationName(property) #>", htmlAttributes: new { @class = "control-label" })
<#
        } 
        else 
        {
#>
            @Html.LabelFor(model => model.<#= property.PropertyName #>, htmlAttributes: new { @class = "control-label" })
<#
        }
        //TITO: SE UTILIZAR CHECKBOX, VERIFICAR NOVAMENTE NOS MODELOS EM C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView
        if (property.IsForeignKey) {
#>
<# 
            if (isControlHtmlAttributesSupported) 
            {
#>
                @Html.DropDownList("<#= property.PropertyName #>", null, htmlAttributes: new { @class = "form-control" })
<#
            } 
            else 
            {
#>
                @Html.DropDownList("<#= property.PropertyName #>", String.Empty)
<#
            }
#>
<#
        } 
        else  if (isControlHtmlAttributesSupported) 
        {
            if (property.IsEnum && !property.IsEnumFlags) 
            {
#>
                @Html.EnumDropDownListFor(model => model.<#= property.PropertyName #>, htmlAttributes: new { @class = "form-control" })
<#
            } 
            else 
            {
#>
                @Html.EditorFor(model => model.<#= property.PropertyName #>, new { htmlAttributes = new { @class = "form-control" } })
<#
            } 
        } 
        else 
        {
#>
                @Html.EditorFor(model => model.<#= property.PropertyName #>)
<#
        }
#>
<# 
        if (isControlHtmlAttributesSupported) 
        {
#>
                @Html.ValidationMessageFor(model => model.<#= property.PropertyName #>, "", new { @class = "help-inline" })
<#        
        } 
        else 
        {
#>
                @Html.ValidationMessageFor(model => model.<#= property.PropertyName #>)
<#      
        }
#>
        </div>
<#
    }
    else
    {
#>
        <div class="form-group">
            @Html.LabelFor(model => model.<#= property.PropertyName #>, htmlAttributes: new { @class = "control-label" })
            @Html.EditorFor(model => model.<#= property.PropertyName #>)
            @Html.ValidationMessageFor(model => model.<#= property.PropertyName #>)
        </div>
<#
    }
}
#>
    <button type="submit" class="btn btn-primary">Criar</button>
}
</div>
</div>
</div>
</div>
@Html.ActionLink("Voltar", "Index", null, new { @class = "btn btn-default" })
</div>
</div>
<#
    if (!IsLayoutPageSelected)  
    {
#>
        </div>
<#
    }
#>
<#
// The following code closes the tag used in the case of a view using a layout page and the body and html tags in the case of a regular view page
#>
<#
if(!IsPartialView && !IsLayoutPageSelected) {
    ClearIndent();
#>
</body>
</html>
<#
}
#>
<#@ include file="ModelMetadataFunctions.cs.include.t4" #>
<# }
    catch(Exception e)
    {
        Trace.TraceError(e.ToString());
        throw;
    }
#>

我有02个问题:

1) DateRange 类型不是由模板自动生成的,只有我的 DateTime 和 string 属性是。2) 我在 /Views/Shared/EditorTemplates 上有 02 个自定义类型模板:DateTime 和 DateRange。两者都不是由 t4 渲染的。

如何让我的自定义类型工作?以及如何按类型创建我的模板也可以工作?

感谢您的任何建议。

4

0 回答 0