Update Visual Studio 2017 MVC View Scaffolding to Bootstrap 4

前端 未结 3 1912
余生分开走
余生分开走 2021-01-04 18:10

I\'ve just updated my Visual Studio 2017 ASP.NET MVC 5 application from Bootstrap v3 to v4. I\'m finding when I add a new edit partial view using scaffolding, it is still us

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 18:41

    An update is not yet available, however to support the edit view scaffolding with Bootstrap 4 in Visual Studio 2017, You have to edit the file Edit.cs.t4 in "%ProgramFiles%\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView"

    • Rename the file to "Edit.cs.backup.t4"
    • Create a new file "Edit.cs.t4" and add the following code, this will support new Bootstrap 4 classes and form controls (Bootstrap custom forms and controls):

    <#@ template language="C#" HostSpecific="True" #>
    <#@ output extension=".cshtml" #>
    <#@ include file="Imports.include.t4" #>
    @model <#= ViewDataTypeName #>
    <#
    // "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#>";
    <#
    }
    #>
    }
    
    

    <#= ViewName#>

    <# } else { #> @{ Layout = null; } <#= ViewName #> <# PushIndent(" "); } #> <# if (ReferenceScriptLibraries) { #> <# if (!IsLayoutPageSelected && IsBundleConfigPresent) { #> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") <# } #> <# else if (!IsLayoutPageSelected) { #> <# } #> <# } #> @using (Html.BeginForm()) { @Html.AntiForgeryToken()

    <#= ViewDataTypeShortName #>


    <# if (isControlHtmlAttributesSupported) { #> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <# } else { #> @Html.ValidationSummary(true) <# } #> <# foreach (PropertyMetadata property in ModelMetadata.Properties) { if (property.Scaffold && !property.IsAssociation) { if (property.IsPrimaryKey) { #> @Html.HiddenFor(model => model.<#= property.PropertyName #>) <# } else if (!property.IsReadOnly) { bool isCheckbox = property.TypeName.Equals(boolType); #>
    <# if (property.IsForeignKey) { #> @Html.LabelFor(model => model.<#= property.PropertyName #>, "<#= GetAssociationName(property) #>", htmlAttributes: new { @class = "col-form-label col-lg-2" }) <# } else if (!isCheckbox) { #> @Html.LabelFor(model => model.<#= property.PropertyName #>, htmlAttributes: new { @class = "col-form-label col-lg-2" }) <# } #>
    <# 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 (isCheckbox) { #>
    <# PushIndent(" "); #> @Html.EditorFor(model => model.<#= property.PropertyName #>, new { htmlAttributes = new { @class = "custom-control-input" } }) @Html.LabelFor(model => model.<#= property.PropertyName #>, htmlAttributes: new { @class = "custom-control-label" }) <# } else 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 = "text-danger" }) <# } else { #> @Html.ValidationMessageFor(model => model.<#= property.PropertyName #>) <# } #> <# if (isCheckbox && isControlHtmlAttributesSupported) { PopIndent(); #>
    <# } #>
    <# } } } #>
    }
    @Html.ActionLink("Back to List", "Index")
    <# if(IsLayoutPageSelected && ReferenceScriptLibraries && IsBundleConfigPresent) { #> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } <# } #> <# else if(IsLayoutPageSelected && ReferenceScriptLibraries) { #> <# } #> <# // 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(); #> <# } #> <#@ include file="ModelMetadataFunctions.cs.include.t4" #>

提交回复
热议问题