jquery - Client side validation of from which is using partial views -
i completed visualization logic form , want use of client side validation asp.net mvc 3 provides. though i'm following examples can't make work , don't know might reason.
here main view :
@model list<dataaccess.mcs_documentfields> @{ viewbag.title = "documents"; } <div id="drawform"> @using (html.beginform("recievedatafromdocument", "forms", formmethod.post)) { @html.validationsummary(true) <table border="1"> <colgroup> <col span="1" style="width: 10%;" /> <col span="1" style="width: 40%;" /> <col span="1" style="width: 25%;" /> <col span="1" style="width: 25%;" /> </colgroup> @html.partial("_partialheader", model) @html.partial("_partialdrawing", model) @html.partial("_partialbody", model) @html.partial("_partialfooter", model) </table> if (viewbag.status == 1) { <button type="submit">save</button> } else { @html.actionlink("back", "index") } } </div>
not here actually. of logic in partials. use data annotations thought i'll have client-side validation default seems not case. have done making sure have
<appsettings> <add key="unobtrusivejavascriptenabled" value="true" /> </appsettings>
added web.config. in view can see i've added
@html.validationsummary(true)
not sure if right place it's there. in example i'm looking there :
<div class="editor-label"> @html.labelfor(model => model.name) </div> <div class="editor-field"> @html.textboxfor(model => model.name) @html.validationmessagefor(model => model.name) </div>
i don't have such <div>
tags , such class names when start application in viewsource can see each input :
name comes db <input data-val="true" data-val-required="the fieldvalue field required." name="[4].fieldvalue" type="hidden" value="name comes db" />
which thought enough client side validation take place. because did not added in 1 of partial views test following :
<div class="editor-label"> @html.displayfor(x => model[i].questiontext) </div> <div class="editor-field"> @html.textbox("datepicker", "", new { @class = "datepicker" }) @html.validationmessagefor(x => model[i].questiontext) </div> @html.hiddenfor(x => model[i].id) //...some code... <div class="editor-field"> @html.editorfor(x => model[i].fieldvalue) @html.validationmessagefor(x => model[i].fieldvalue) </div> @html.hiddenfor(x => model[i].id) //...more code..
but 2 fields doesn't generate error when validation fails. guess i'm either missing or i'm doing wrong. doubt if kind of validation works way - partials?
in order client side validation in mvc (i work on mvc4 think it's same in case), there steps check:
in web.config
<add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" />
scripts have in layout (or master page)
<script src="/scripts/jquery.validate.js"></script> <script src="/scripts/jquery.validate.unobtrusive.js"></script> <script src="/scripts/jquery.unobtrusive-ajax.js"></script>
give model validation attributes error messages display (for example)
public class contactform { [display(name = "mail :"), required(allowemptystrings = false, errormessage = "mail required"), regularexpression("^(|[a-za-z0-9_.+-]+@[a-za-z0-9-]+[.][a-za-z]{2,3})$", errormessage = "mail not valid"), remote("isvalidmail", "validate", httpmethod="get")] /* remote validation */ public string mail { get; set; } [display(name = "message :"), required(allowemptystrings = false, errormessage = "message required"), stringlength(maximumlength: 400, errormessage="message long")] public string message { get; set; } public contactform() { }
}
in view, show error message
@html.validationmessagefor(x=>x.mail)
or
@html.validationsummary(false, null, new { @id = "validationsummary" })
as did.
actually, works that, in case of ajax forms within partial view, need rebind validation on page load (in success event of ajax call or $(function(){} of partial view)
///because page loaded ajax, validation rules lost, have rebind them: $('#form').removedata('validator'); $('#form').removedata('unobtrusivevalidation'); $("#form").each(function () { $.data($(this)[0], 'validator', false); }); //enable display error messages $.validator.unobtrusive.parse("#form");
be careful partial views not double scripts , enable validation. best regards
Comments
Post a Comment