c# - 服务器和客户端验证差异增加了额外的跨度

标签 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="field-validation-error text-danger">Broken</span>

问题是

<span for="Location" class="">

他们是一种让服务器端验证添加额外跨度的方法吗? 或者他们是一种让客户端验证删除额外跨度的方法吗?

感谢任何帮助:)

模型

    public class LocationModel
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string Location { get; set; }
}

Controller

[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")
}

最佳答案

不确定为什么会出现问题。

但这里是生成额外跨度的代码。代码在 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 || "" );

        ...
    },

errorElement 的默认值为 label,但 jquery.validate.unobtrusive.js 将其更改为 span。 您可以通过多种方式更改此行为。

但这里有一个,改变 jquery.validation.unobtrusive.js

来自:

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());
    }

    ...

}

希望这对您有所帮助!

关于c# - 服务器和客户端验证差异增加了额外的跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30232701/

相关文章:

c# - 如何根据某个数字重复特定顺序的数组元素?

JavaScript 在我建议之前就加载了

asp.net-mvc - Entity Framework 和asp.net mvc

javascript - React PropTypes - 以数字为键的形状

javascript - 在 jsp 中调用 onclick 方法之前进行表单验证

c# - 如何查看用户是否未在 C# 中输入有效条目

c# - 第一个元素作为快速排序中的枢轴

c# - 为什么 C# 不需要在类定义后使用分号?

javascript - 在 JavaScript 中使用 Razor

PHP正则表达式来验证输入的多次出现