细节和编辑视图中的对齐问题
Alignment Problems in Details and Edit View
我不太明白如何让它与 mvc 6 正确对齐。"dropdown" 有一个 CSS 标签,但它只是位置:相对并且不做任何事情全部。这都是使用 Entity Framework.
自动生成的脚手架
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PersonId)
<div class="form-group">
@Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="dropdown">
@Html.DropDownList("RoleList", null, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Roles, "", 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>
页面来源
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit - My ASP.NET Application</title>
<link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">BDP New User Tool</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/Location">Locations</a></li>
<li><a href="/Country">Countries</a></li>
<li><a href="/Department">Departments</a></li>
</ul>
<form action="/Account/LogOff" class="navbar-right" id="logoutForm" method="post"><input name="__RequestVerificationToken" type="hidden" value="wgihBWaaShrDRYvgLvQl0KJsu0kxsOUARHb-vQI02G1yA71ip-y44MmYKFuXBbkLGCvzB1TeOyyC-Zz5NGEWZm9JR-7iRCsmuAv9osMEobzooZglgT4CeWIvvIlKu6wnTLKcAlJo8DW-ci9aD8kV6A2" /> <ul class="nav navbar-nav navbar-right">
<li>
<a href="/Manage" title="Manage">Hello nathan.mckaskle@email.com!</a>
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
</form>
</div>
</div>
</div>
<div class="container body-content">
<h2>Edit</h2>
<form action="/people/Edit/1" method="post"><input name="__RequestVerificationToken" type="hidden" value="P-S-hQ1lZkis-H2u8xV_ovw4chg0_lTzY9gx3h6xJn77k4HqJFQipnnKp5D2TeSFeTciZeACc9iljdDwmKErGepreF-kc3RMf9d1wFPAUlEaqbhAQ8sOBgcXmr0XlRayMEZt1lfA8OfqkpG3_xhN2A2" /> <div class="form-horizontal">
<h4>Person</h4>
<hr />
<input data-val="true" data-val-number="The field PersonId must be a number." data-val-required="The PersonId field is required." id="PersonId" name="PersonId" type="hidden" value="0" />
<div class="form-group">
<label class="control-label col-md-2" for="FullName">Full Name</label>
<div class="col-md-10">
Nathan McKaskle
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Email">Email</label>
<div class="col-md-10">
nathan.mckaskle@email.com
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Roles">Roles</label>
<div class="dropdown">
<select class="form-control" id="RoleList" name="RoleList"><option selected="selected" value="71758b95-b2b1-410a-8267-ebbd26905102">Admin</option>
<option value="5c0c5cba-fb44-4320-8d9c-e76544e77d60">Basic</option>
<option value="c34e731d-30a2-42e5-9d64-d1c2bd33ef17">Create</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="Roles" data-valmsg-replace="true"></span>
</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>
</form>
<div>
<a href="/people">Back to List</a>
</div>
<hr />
<footer>
<p>© 2017 - My ASP.NET Application</p>
</footer>
</div>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Chrome","requestId":"fa5c07ac3f3645a88405d2c43af159f5"}
</script>
<script type="text/javascript" src="http://localhost:22384/f8c630454ed048b89d34363a3c17c7fe/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>
UPDATE:这是我的 HTML 的更新,由下面的答案推荐。这使得页面上的每个元素都散落在各处。请参阅下一个屏幕截图。
@model NewUserTool.Models.PersonViewModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PersonId)
<div class="form-control-static">
@Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.FullName, new { @class = "form-control" })
</div>
</div>
<div class="form-control-static">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-control-static">
@Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="dropdown">
@Html.DropDownList("RoleList", null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Roles, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-control-static">
<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>
是否这样做:
DisplayFor
不像 EditorFor
那样接受 htmlAttributes
。对于 FullName
之类的字符串,默认显示只是转储字符串值而不包含任何 HTML,因此在大多数情况下,没有任何内容可以实际添加属性。但是,即使这确实有效,你仍然会在这里使用错误的 class,因为你正在寻找的实际上是 form-control-static
哪些样式文本要对齐,就好像它是输入 class form-control
。所以你实际需要的代码是:
<p class="form-control-static">@Html.DisplayFor(m => m.Email)</p>
此外,您的下拉菜单未对齐,因为您没有正确传递 HTML 属性。匿名对象语法中的匿名对象用于 EditorFor
。 EditorFor
中的该参数实际上用于附加视图数据。实际上,您正在向 ViewData
添加一个名为 htmlAttributes
的键,默认编辑器模板会在 ViewData
中查找此键并对其进行处理。但是,对于像 TextBoxFor
、DropDownListFor
等助手,参数本身是针对 HTML 属性的。结果,您要告诉它添加一个名为 htmlAttributes
的属性并为其赋予该匿名对象的值,我什至不确定助手会用它做什么。长短不一,把代码改成:
@Html.DropDownList("RoleList", null, new { @class = "form-control" })
在您的 html
中,您从未正确包装 dropdown
。如果您有一行,请务必声明列,总共 12。
<div class="form-group">
<label class="control-label col-md-2" for="Roles">Roles</label>
<div class="col-md-10">
<div class="dropdown">
<select class="form-control" id="RoleList" name="RoleList"><option selected="selected" value="71758b95-b2b1-410a-8267-ebbd26905102">Admin</option>
<option value="5c0c5cba-fb44-4320-8d9c-e76544e77d60">Basic</option>
<option value="c34e731d-30a2-42e5-9d64-d1c2bd33ef17">Create</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="Roles" data-valmsg-replace="true"></span>
</div>
</div>
</div>
此外,我确实将您的文本包装到 p
标签中,并应用了与标签相同的上边距。
我不太明白如何让它与 mvc 6 正确对齐。"dropdown" 有一个 CSS 标签,但它只是位置:相对并且不做任何事情全部。这都是使用 Entity Framework.
自动生成的脚手架<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PersonId)
<div class="form-group">
@Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="dropdown">
@Html.DropDownList("RoleList", null, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Roles, "", 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>
页面来源
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit - My ASP.NET Application</title>
<link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">BDP New User Tool</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/Location">Locations</a></li>
<li><a href="/Country">Countries</a></li>
<li><a href="/Department">Departments</a></li>
</ul>
<form action="/Account/LogOff" class="navbar-right" id="logoutForm" method="post"><input name="__RequestVerificationToken" type="hidden" value="wgihBWaaShrDRYvgLvQl0KJsu0kxsOUARHb-vQI02G1yA71ip-y44MmYKFuXBbkLGCvzB1TeOyyC-Zz5NGEWZm9JR-7iRCsmuAv9osMEobzooZglgT4CeWIvvIlKu6wnTLKcAlJo8DW-ci9aD8kV6A2" /> <ul class="nav navbar-nav navbar-right">
<li>
<a href="/Manage" title="Manage">Hello nathan.mckaskle@email.com!</a>
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
</form>
</div>
</div>
</div>
<div class="container body-content">
<h2>Edit</h2>
<form action="/people/Edit/1" method="post"><input name="__RequestVerificationToken" type="hidden" value="P-S-hQ1lZkis-H2u8xV_ovw4chg0_lTzY9gx3h6xJn77k4HqJFQipnnKp5D2TeSFeTciZeACc9iljdDwmKErGepreF-kc3RMf9d1wFPAUlEaqbhAQ8sOBgcXmr0XlRayMEZt1lfA8OfqkpG3_xhN2A2" /> <div class="form-horizontal">
<h4>Person</h4>
<hr />
<input data-val="true" data-val-number="The field PersonId must be a number." data-val-required="The PersonId field is required." id="PersonId" name="PersonId" type="hidden" value="0" />
<div class="form-group">
<label class="control-label col-md-2" for="FullName">Full Name</label>
<div class="col-md-10">
Nathan McKaskle
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Email">Email</label>
<div class="col-md-10">
nathan.mckaskle@email.com
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Roles">Roles</label>
<div class="dropdown">
<select class="form-control" id="RoleList" name="RoleList"><option selected="selected" value="71758b95-b2b1-410a-8267-ebbd26905102">Admin</option>
<option value="5c0c5cba-fb44-4320-8d9c-e76544e77d60">Basic</option>
<option value="c34e731d-30a2-42e5-9d64-d1c2bd33ef17">Create</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="Roles" data-valmsg-replace="true"></span>
</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>
</form>
<div>
<a href="/people">Back to List</a>
</div>
<hr />
<footer>
<p>© 2017 - My ASP.NET Application</p>
</footer>
</div>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Chrome","requestId":"fa5c07ac3f3645a88405d2c43af159f5"}
</script>
<script type="text/javascript" src="http://localhost:22384/f8c630454ed048b89d34363a3c17c7fe/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>
UPDATE:这是我的 HTML 的更新,由下面的答案推荐。这使得页面上的每个元素都散落在各处。请参阅下一个屏幕截图。
@model NewUserTool.Models.PersonViewModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PersonId)
<div class="form-control-static">
@Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.FullName, new { @class = "form-control" })
</div>
</div>
<div class="form-control-static">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-control-static">
@Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="dropdown">
@Html.DropDownList("RoleList", null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Roles, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-control-static">
<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>
是否这样做:
DisplayFor
不像 EditorFor
那样接受 htmlAttributes
。对于 FullName
之类的字符串,默认显示只是转储字符串值而不包含任何 HTML,因此在大多数情况下,没有任何内容可以实际添加属性。但是,即使这确实有效,你仍然会在这里使用错误的 class,因为你正在寻找的实际上是 form-control-static
哪些样式文本要对齐,就好像它是输入 class form-control
。所以你实际需要的代码是:
<p class="form-control-static">@Html.DisplayFor(m => m.Email)</p>
此外,您的下拉菜单未对齐,因为您没有正确传递 HTML 属性。匿名对象语法中的匿名对象用于 EditorFor
。 EditorFor
中的该参数实际上用于附加视图数据。实际上,您正在向 ViewData
添加一个名为 htmlAttributes
的键,默认编辑器模板会在 ViewData
中查找此键并对其进行处理。但是,对于像 TextBoxFor
、DropDownListFor
等助手,参数本身是针对 HTML 属性的。结果,您要告诉它添加一个名为 htmlAttributes
的属性并为其赋予该匿名对象的值,我什至不确定助手会用它做什么。长短不一,把代码改成:
@Html.DropDownList("RoleList", null, new { @class = "form-control" })
在您的 html
中,您从未正确包装 dropdown
。如果您有一行,请务必声明列,总共 12。
<div class="form-group">
<label class="control-label col-md-2" for="Roles">Roles</label>
<div class="col-md-10">
<div class="dropdown">
<select class="form-control" id="RoleList" name="RoleList"><option selected="selected" value="71758b95-b2b1-410a-8267-ebbd26905102">Admin</option>
<option value="5c0c5cba-fb44-4320-8d9c-e76544e77d60">Basic</option>
<option value="c34e731d-30a2-42e5-9d64-d1c2bd33ef17">Create</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="Roles" data-valmsg-replace="true"></span>
</div>
</div>
</div>
此外,我确实将您的文本包装到 p
标签中,并应用了与标签相同的上边距。