Wednesday 15 August 2012

c# - Validate Model data before ajax method call -


I have a partial view on my application. Partial view of user registration functionality (not using any form tag) I am calling the action method of the controller using AJAX-jquery amp; Before calling the AJAX method, there is a need to validate user details in customer details. How can I do this?

The model needs to be valid before the AJAX call (do not use any form tag).

  @ model MVC4Demo.Models.Student & lt; Script src = "~ / script / jQuery-1.7.1.min.js" & gt; & Lt; / Script & gt; & Lt; Script src = "~ / scripts / jquery.validate.min.js" & gt; & Lt; / Script & gt; & Lt; Script src = "~ / script / jquery.validate.unobtrusive.min.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; Function onClientClick () {var obj = {first name: "name", city: "abc"}; $ Type: "Jason", Content Type: "Application / Jason; Charset = UTF-8", type: "post", datatype: "AJAX", "AJAX", "AZ" ({url: "/ home / Ajax demo", data: JSON.rringes (OBJ), type: "post" Success: Work (results) {alert ('success');}, error: work () {warning ("error");}}); } & Lt; / Script & gt; & Lt; Div id = "main" & gt; & Lt; Label & gt; Students & lt; / Labels & gt; & Lt; Div class = "editor-label" & gt; @htmllabelfor (model = & gt; model first name) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model => model first name) @html. Validation message (model = & gt; model first name) & lt; / Div & gt; & Lt; Div class = "editor-label" & gt; @htmllabelfor (model => model.lostname) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model => model.LastName) @ html.ValidationMessageFor (model => model.LastName) & lt; / Div & gt; & Lt; Div class = "editor-label" & gt; @HTML LabelFor (model = & gt; model.ct) & lt; / Div & gt; & Lt; Div class = "editor-field" & gt; @ Html.EditorFor (model => model.City) @ Html.ValidationMessageFor (model = & gt; model.City) & lt; / Div & gt; & Lt; P & gt; & Lt; Input type = "submit" onclick = "onClientClick ()" value = "create" /> & Lt; / P & gt; & Lt; / Div & gt; & Lt; Div & gt; @html.exe link ("back to list", "index") & lt; / Div & gt;    

After placing your model in the form:

 < Code> & lt; Script & gt; $ ('' Myuf '') ($ ('' myform ''). Unbind ('submit'); $ ('# myForm'). Bind ('submit', function) {var form = this; if ($ ($ "MyForm") Valid ()) {$ .post ('/ Home / Ajaxdemo', $ (form) .Serialize (), function (data) {if (data.success) {alert ('success');} And {warning ("error");}});} return false;})});   



No comments:

Post a Comment