我的_layout中有一个导航栏,其中包含一个下拉菜单,尽管存在JQuery验证脚本(通过bundle config),并且该模型具有正确的设置,但在该下拉菜单中仍呈现了局部视图(包含登录表单)数据批注,表单输入将无法验证,但是如果部分文本在正文中呈现,则验证工作正常。
我认为那是关于布局的某些问题(或者表单处于下拉菜单中)导致了问题。
布局的重要部分...
@Scripts.Render("~/bundles/jquerybase")
@Scripts.Render("~/bundles/jqueryajax")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@Styles.Render("~/Styles/bootstrap")
@Styles.Render("~/Styles/css")
<nav class="navbar navbar-inverse navbar-fixed-top" id="MainNavbar">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Logo</a>
</div>
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
<ul class="nav navbar-nav navbar-right float-right">
@if (!User.Identity.IsAuthenticated)
{
<li>
<a href="#" class="dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-log-in"></span> Login
</a>
<div class="dropdown-menu" id="dropdownLogon">
@Html.Partial("~/Views/User/Logon.cshtml", new SiteProject.Models.UserModel())
</div>
}
</li>
</ul>
</div>
</nav>
我的局部视图:
@model SiteProject.Models.UserModel
@using (Html.BeginForm("Logon", "User", FormMethod.Post, new { htmlAttributes = new { Id = "logonForm", name = "logonForm" } }))
{
@Html.AntiForgeryToken()
<form class="px-4 py-3">
<div class="form-group">
@Html.Label("Username", htmlAttributes: new { id = "UserNameLabel" })
@Html.EditorFor(m => m.UserName, new { htmlAttributes = new { id = "Username", @class = "form-control", } })
@Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.Label("Password", htmlAttributes: new { id = "PasswordLabel" })
@Html.EditorFor(m => m.Password, new { htmlAttributes = new { @type = "password", id = "Password", @class = "form-control", } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<br />
<div class="dropdown-divider"></div>
<div>
<a class="dropdown-item" href="#">Forgot password?</a>
</div>
}
谁能阐明原因以及解决方案。
谢谢。