javascript - How to serialize forms and post using jQuery Ajax - Stack Overflow

I'm trying to remove records from my DB......this is how my form looks like.........@using (Html.

I'm trying to remove records from my DB...... this is how my form looks like.........

@using (Html.BeginForm("RemoveDoctor", "Doctor", FormMethod.Post, new { @id = "form" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.Name)
    <div class="form-actions no-color">
        <input type="submit" value="Delete" class="btn btn-default" id="submit" /> |

    </div>
}

I'm trying to get these records from the view and pass to my controller Action method............................. i'm trying to serialize this form and send it to that action method as following...................

var jsonObj = $('#form').serialize();

it serialize the form put my Ajax POST function wont run with that result...... it just gives me an error!!!!................. I just need to pass that serialize value to my Action method............... This is how my Script looks like.....................

$('#submit').click(function () {

     var jsonObj = $('#form').serialize();
     alert(jsonObj);

     $.ajax({
           type: "POST",
           url: '../Doctor/RemoveDoctor',
           data: JSON.stringify({ "doctor": jsonObj }),
           success: function (data) {
                 alert(data.Message);
           },
           error: function () {
                  alert("Error!!!");
           }
      });
      return false;

});

This is how my action method looks like....................

  public ActionResult RemoveDoctor(DoctorModel doctor)
  {
      bool confirmationResult = doctorManager.RemoveDoctor(doctor.Id);
      string displayMessage = string.Empty;
      if (confirmationResult == true)
           displayMessage = "You have successfully removed your record!!";
      else
           displayMessage = "Error!! Some Thing Went Wrong, Please Try Again!!";

      return Json(new { Message = displayMessage });

  }

I'm trying to send this 'displayMessage' to my jQuery code........ please some give me an idea how to solve this....... thanks!!!!!

I'm trying to remove records from my DB...... this is how my form looks like.........

@using (Html.BeginForm("RemoveDoctor", "Doctor", FormMethod.Post, new { @id = "form" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.Name)
    <div class="form-actions no-color">
        <input type="submit" value="Delete" class="btn btn-default" id="submit" /> |

    </div>
}

I'm trying to get these records from the view and pass to my controller Action method............................. i'm trying to serialize this form and send it to that action method as following...................

var jsonObj = $('#form').serialize();

it serialize the form put my Ajax POST function wont run with that result...... it just gives me an error!!!!................. I just need to pass that serialize value to my Action method............... This is how my Script looks like.....................

$('#submit').click(function () {

     var jsonObj = $('#form').serialize();
     alert(jsonObj);

     $.ajax({
           type: "POST",
           url: '../Doctor/RemoveDoctor',
           data: JSON.stringify({ "doctor": jsonObj }),
           success: function (data) {
                 alert(data.Message);
           },
           error: function () {
                  alert("Error!!!");
           }
      });
      return false;

});

This is how my action method looks like....................

  public ActionResult RemoveDoctor(DoctorModel doctor)
  {
      bool confirmationResult = doctorManager.RemoveDoctor(doctor.Id);
      string displayMessage = string.Empty;
      if (confirmationResult == true)
           displayMessage = "You have successfully removed your record!!";
      else
           displayMessage = "Error!! Some Thing Went Wrong, Please Try Again!!";

      return Json(new { Message = displayMessage });

  }

I'm trying to send this 'displayMessage' to my jQuery code........ please some give me an idea how to solve this....... thanks!!!!!

Share Improve this question asked Feb 25, 2015 at 7:19 DayanDayan 7515 gold badges16 silver badges27 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

Try this

 $.ajax({
           type: "POST",
           url: '../Doctor/RemoveDoctor',
           data: $('#form').serialize(),
           success: function (data) {
                 alert(data.Message);
           },
           error: function () {
                  alert("Error!!!");
           }
      });

It will serialize your form.

Use only $('#form').serialize() for serialization.

Edit

If you don't want to refresh page then you should use type="button" instead type="submit"

And

You should do this also

[HttpPost]
public ActionResult RemoveDoctor(DoctorModel doctor)
  {
      //...................
      return Json(new { Message = displayMessage } , JsonRequestBehavior.AllowGet);

  }

And change ajax error function to this (For getting error )

error: function(jqXHR, textStatus, errorThrown)
{
  alert("Error: "+errorThrown+" , Please try again");   
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745576708a4634008.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信