c# - server and client side validation differences adding extra span -
i have validation both on server side , client side when validation added on server side different structure when on client side
client
<span data-valmsg-replace="true" data-valmsg-for="location" class="text-danger field-validation-error"><span for="location" class="">the location field required.</span></span>
server side
<span data-valmsg-replace="true" data-valmsg-for="location" class="field-validation-error text-danger">broken</span>
the problem
<span for="location" class="">
is way make server side validation add span? or way make client side validation remove span?
any appreciated :)
model
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(); }
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 list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
not sure why problem.
but here code generating span. code in 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 || "" ); ... },
the default value errorelement label, jquery.validate.unobtrusive.js changes span. there several ways can change behavior.
but here one, change jquery.validation.unobtrusive.js
from:
function onerror(error, inputelement) { // 'this' form element ... if (replace) { container.empty(); error.removeclass("input-validation-error").appendto(container); } ... }
to:
function onerror(error, inputelement) { // 'this' form element ... if (replace) { container.empty().html(error.html()); } ... }
hope helps!
Comments
Post a Comment