jquery - validate form on MVC4 but avoid submit -
i have set of fields on view on aplication, need validate fields before refreshing jqgrid, need prevent submit functiuon post server, need form validate fields , call jquery funcition. dont know how it.
until have somthing like:
@using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) @html.hiddenfor(m => m.scheduledidpersonaseleccionada) @html.validationmessagefor(model => model.scheduledidpersonaseleccionada) <button type="submit" class="btntexto" id="buscarporpersona"> <img src="~/images/icons/search.png" /> <span>@html.raw(res_string.buscarturnos)</span> </button> }
and jquery function called reloadgrid()
, need when press button validation cheks fields (1 on example) if ok call reloadgrid()
function, validation not working
pd jquery validation libraries loaded ok
there 3 ways use. first use @rosdi's code , add validation event
if($('#scheduledidpersonaseleccionada').val() == ""){ alert('something'); }
you can use ajax call server , send fields want validate
$.ajax({ type: "post", url: "@(url.action("action", "controller"))", data: { data: 'data', data1: 'data1'}, contenttype: "application/json; charset=utf-8", datatype: "json", success: function (result) { if(result.success){ }else{ } } });
you mentioned avoiding submit use that. put attribute on field
[required] public string scheduledidpersonaseleccionada { get; set; }
and in controller check model state
if(modelstate.isvalid){ }else{ return view(model); }
the third way easiest since sends model , checking done attributes have sent. helps.
Comments
Post a Comment