VS 2015社区 - 自定义脚手架 - MVC 4 - 自定义类型

时间:2016-02-21 01:31:02

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-scaffolding t4scaffolding

我尝试在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渲染的。

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

感谢您的任何建议。

0 个答案:

没有答案