2

I'm trying to use custom scaffolding on Visual Studio 2015 Community with MVC 4.

My model has a custom type, a class created by me.

Sample:

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

and my create.cs.t4 template for scaffold is:

<#@ 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;
    }
#>

I have 02 issues:

1) DateRange type is not auto-generated by template, just my DateTime and string property are. 2) I have 02 custom type templates on /Views/Shared/EditorTemplates: DateTime and DateRange. Both are not rendered by t4.

How to get my custom type to work? And how to my template by type can work also?

Thanks for any advice.

Tito
  • 21
  • 2

0 Answers0