我正在使用asp.net mvc 5(C#)作为我的应用程序。客户端验证在本地工作正常,但在服务器中无效。
以下是来自BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"
));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"
));
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-{version}.js"
));
以下是_Layout.cshtml(在底部添加):
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jqueryval")
@RenderSection("scripts", required: false)
</body>
</html>
以下是我的登录页面:
@using (Html.BeginForm("SignIn", "User", FormMethod.Post, new { id = "signInForm" }))
{
<div class="titleBg"><h2>Sign In</h2></div>
<table>
<tr><td class="spacer50 taC" colspan="2">@Html.ValidationSummary(true)</td></tr>
<tr>
<td style="width:40%;" class="taR pRight10">
<label for="EmailAddress">Email</label>
</td>
<td class="fieldName">
@Html.TextBoxFor(model => model.EmailAddress, new { maxlength = "100", @class = "field mLeft10 email", style = "", @tabindex = "1" })
@Html.ValidationMessageFor(model => model.EmailAddress)
</td>
</tr>
<tr>
<td class="taR">
<label for=" password">Password</label>
</td>
<td class="fieldName">
@Html.PasswordFor(model => model.Password, new { maxlength = "12", @class = "field mLeft10 pass", style = "", @tabindex = "2" })
@Html.ValidationMessageFor(model => model.Password)
</td>
</tr>
<tr>
<td colspan="2" class="spacer10"></td>
</tr>
<tr><td></td>
<td class="taL">
<input id="btnSignIn" type="submit" value="Sign In" class="mLeft10" />
</td>
</tr>
</table>
}
以下是EmailAddress字段在Local中的呈现方式:
<input type="text" value="" tabindex="1" style="" name="EmailAddress" maxlength="100" id="EmailAddress" data-val-required="Email Address is required" data-val-email="Email Address is invalid" data-val="true" class="field mLeft10 email">
以下是EmailAddress字段在服务器中的呈现方式:
<input type="text" value="" tabindex="1" style="" name="EmailAddress" maxlength="100" id="EmailAddress" class="field mLeft10 email">
本地和服务器配置:
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
以上实现在本地工作完美,但我们将此移动到服务器,客户端验证不起作用。当我们在服务器中运行网站时,缺少data-val属性。
有人可以建议我在这里缺少什么吗?
答案 0 :(得分:0)
我遇到了同样的问题。最后我的Web.Config转换设置为“替换”默认的web.config appSetting部分。
一旦我确定这些设置在配置中,它就起作用了:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>