C# 服务器端和客户端验证差异增加了额外的跨度
我有服务器端和客户端的验证 当在服务器端添加验证时,我得到了与在客户端进行验证时不同的结构 客户C# 服务器端和客户端验证差异增加了额外的跨度,c#,asp.net-mvc,validation,asp.net-mvc-5,unobtrusive-validation,C#,Asp.net Mvc,Validation,Asp.net Mvc 5,Unobtrusive Validation,我有服务器端和客户端的验证 当在服务器端添加验证时,我得到了与在客户端进行验证时不同的结构 客户 <span data-valmsg-replace="true" data-valmsg-for="Location" class="text-danger field-validation-error"><span for="Location" class="">The Location field is required.</span></span>
<span data-valmsg-replace="true" data-valmsg-for="Location" class="text-danger field-validation-error"><span for="Location" class="">The Location field is required.</span></span>
控制器
[HttpPost]
public ActionResult LocationView(LocationModel lm)
{
ModelState.AddModelError("Location", "Broken");
return View();
}
看法
@model WebApplication1.Models.LocationModel
@{
ViewBag.Title=“查看”;
}
看法
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
位置模型
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Location,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Location,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.Location,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
不确定为什么会出现问题
但下面是生成额外跨度的代码。代码在jquery.validate.js中
showLabel: function( element, message ) {
...
} else {
// create error element
error = $( "<" + this.settings.errorElement + ">" )
.attr( "id", elementID + "-error" )
.addClass( this.settings.errorClass )
.html( message || "" );
...
},
致:
希望这有帮助 这肯定是有人以前遇到过的:(为什么这是一个问题?这是默认行为。你想做什么?太好了!感谢你的回答-作为对原因的回应-我相信使用两种不同的语法来显示错误有点不一致。当然,没有额外跨度的方式足够具体
public class LocationModel
{
[Required]
public int Id { get; set; }
[Required]
public string Location { get; set; }
}
[HttpPost]
public ActionResult LocationView(LocationModel lm)
{
ModelState.AddModelError("Location", "Broken");
return View();
}
@model WebApplication1.Models.LocationModel
@{
ViewBag.Title = "View";
}
<h2>View</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LocationModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
showLabel: function( element, message ) {
...
} else {
// create error element
error = $( "<" + this.settings.errorElement + ">" )
.attr( "id", elementID + "-error" )
.addClass( this.settings.errorClass )
.html( message || "" );
...
},
function onError(error, inputElement) { // 'this' is the form element
...
if (replace) {
container.empty();
error.removeClass("input-validation-error").appendTo(container);
}
...
}
function onError(error, inputElement) { // 'this' is the form element
...
if (replace) {
container.empty().html(error.html());
}
...
}