将Visual Studio 2017 MVC视图脚手架更新为Bootstrap 4 [英] Update Visual Studio 2017 MVC View Scaffolding to Bootstrap 4
问题描述
我刚刚将我的Visual Studio 2017 ASP.NET MVC 5应用程序从Bootstrap v3更新到了v4.我在添加新的部分编辑视图时发现使用脚手架,它仍在使用Bootstrap v3 CSS类名称作为表单.有没有一种方法可以将脚手架更新为使用BS v4?
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 using the Bootstrap v3 CSS class names for the form. Is there a way to update the scaffolding to use BS v4?
修改
我在说什么似乎有些困惑.
There seems to be some confusion about what I'm talking about.
在Visual Studio 2017中的MVC项目中的解决方案资源管理器中,右键单击Views folder > Add > View... > MVC 5 View > Click Add
.
In Visual Studio 2017, in an MVC project, in Solution Explorer, right click the Views folder > Add > View... > MVC 5 View > Click Add
.
这将弹出添加视图"对话框.我输入View名称,选择Edit Template,然后选择LoginVm
作为Model类. Visual Studio会生成此标记.此过程称为脚手架.
This brings up the Add View dialog. I type my View name, choose the Edit Template and choose, for this example, LoginVm
as the Model class. Visual Studio generates this markup. This process is called scaffolding.
@model Demo.ViewModels.LoginVm
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LoginVm</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
请注意正在使用的Bootstrap 3类,例如form-label
和col-md-offset-2
.这些已在Bootstrap 4中删除.类似地,如果要创建一个新的Layout页面,它将生成一个Bootstrap 3导航栏,该导航栏在Bootstrap 4中无效.
Notice the Bootstrap 3 classes in use such as form-label
and col-md-offset-2
. These were removed in Bootstrap 4. Similarly, if you were to create a new Layout page, it would generate a Bootstrap 3 Navbar which is invalid in Bootstrap 4.
所以我想问是否有一种方法(短于编写自定义脚手架)来更新Visual Studio,以停止输出特定于Bootstrap 3的标记,而最好是输出Bootstrap 4标记?
So I'm asking if there is a way (short of writing a custom scaffolder) to update Visual Studio to stop outputting Bootstrap 3 specific markup and, ideally, output Bootstrap 4 markup instead?
推荐答案
要将ASP .NET MVC 5项目导航栏修复/更新到Bootstrap 4,您必须手动更新代码,如下所示:
To fix/update the ASP .NET MVC 5 project navbar to Bootstrap 4 you have to update your code manually as follows:
- 视图->共享-> _Layout.cshtml.
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target=".navbar-collapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav mr-auto">
<li class="nav-item">@Html.ActionLink("Home", "Index", "Home", null, new { @class = "nav-link" })</li>
<li class="nav-item">@Html.ActionLink("About", "About", "Home", null, new { @class = "nav-link" })</li>
<li class="nav-item">@Html.ActionLink("Contact", "Contact", "Home", null, new { @class = "nav-link" })</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</nav>
然后,如果您不使用登录"部分,则可以将其删除.否则,请将_LoginPartial.cshtml更改为:
Then if you don’t use the Login partial, you can remove it. Otherwise, change the _LoginPartial.cshtml to:
@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav">
<li class="nav-item">
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage", @class = "nav-link" })
</li>
<li class="nav-item"><a class="nav-link" href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
else
{
<ul class="nav navbar-nav">
<li class="nav-item">@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink", @class = "nav-link" })</li>
<li class="nav-item">@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink", @class = "nav-link" })</li>
</ul>
}
最后,只需从Content/Site.css中删除下一行:
And last just remove the next lines from Content/Site.css:
/*delete this*/
body {
padding-top: 50px;
padding-bottom: 20px;
}
这篇关于将Visual Studio 2017 MVC视图脚手架更新为Bootstrap 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!