my jquery ajax does not work in asp.net mvc -


i want send data via jquery ajax in asp.net mvc

but when function create called error section excuted

here java script

    function create() {         $.ajax({             url: "@url.action(actionname:"create",controllername:"tag",routevalues:new { area="admin"})",             contenttype: "application/json",             type: "post",             datatype: "json",             data: $("#myform").serialize(),             success: function (response) {                 eval(response.script);             }, error: function () {                 alert("error");             }         }); 

here action method

    [validateantiforgerytoken,httppost, ajaxonly]     public actionresult create(tag tag)     {                     jsondata data = new jsondata();         if (modelstate.isvalid)         {             if (tagoperation.where(x => x.text == tag.text) != null)             {                 try                 {                     tagoperation.add(tag);                     data.script = messagebox.show("message", messagetype.success).script;                     return redirecttoaction(actionname: "index", controllername: "tag", routevalues: new { area = "admin" });                 }                 catch (exception ex)                 {                     data.script = messagebox.show("message", messagetype.error).script;                     return json(data);                 }             }         }         data.script = messagebox.show(modelstate.geterrors(), messagetype.warning).script;         return json(data);     } 

and here view send data via jquery ajax

@using (html.beginform("create", "tag", formmethod.post, new { id = "myform" })){     @html.antiforgerytoken() <div class="form-horizontal">             @html.validationsummary(true, "", new { @class = "text-danger" })     <div class="form-group">         @html.labelfor(model => model.text, htmlattributes: new { @class = "control-label col-md-2" })         <div class="col-md-10">             @html.editorfor(model => model.text, new { htmlattributes = new { @class = "form-control" } })             @html.validationmessagefor(model => model.text, "", new { @class = "text-danger" })         </div>     </div>      <div class="form-group">         <div class="col-md-offset-2 col-md-10">             <input onclick="create();" type="button" value="save" class="btn btn-default" />         </div>     </div> </div> } 

try adding [httppost] attribute action result method (by default action results use get)

[httppost]   public actionresult create(tag tag){..} 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -