Ajax POST jQuery issue

Ajax POST jQuery issue

本文关键字:issue jQuery POST Ajax      更新时间:2024-04-13

我正试图从数据库中删除一个元素。。。。我已经在我的MVC控制器中创建了这个操作方法。。。。。。。

    [HttpPost]
    public ActionResult RemoveDoctor(DoctorModel doctor)
    {
        //confirming whether element is removed from DB
        bool confirmationResult = repo.RemoveDoctor(doctor.Id);
        string str = null;
        if (confirmationResult == true)
            str = "You have successfully Removed your record!!";
        else
            str = "Error!! Some Thing Went Wrong, Please Try Again!!";
        return Json(str);
    }

我需要看看它是否被删除,并通知用户它是否已成功删除。。。。。。。。。。。。但问题是,当我以Json的身份返回时,字符串不会移动到我的jQuery POST函数,它只会在浏览器中显示我的字符串。。。。。。。。。。。。。。。。。。。。。。。例如,它只会输出以下消息:"您已成功删除您的记录!!"。。。。。。。。。。。。。。。。。。。。。这就是我的功能:

$('#submit').click(function () {
    var modelDataJSON = @Model;
    $.ajax({
          type: "POST",
          url: "../Doctor/RemoveDoctor",
          data: {"doctor" : modelDataJSON},
          success: function (data) {
               alert(data);
          },
          dataType: "json",
          error: function () {
               alert("Error!!!");
          }
     });
  });

这就是我的html表单的样子:

@using (Html.BeginForm("AddDoctor", "Doctor"))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4></h4>
        <hr />
        @Html.ValidationSummary(true)
        <div class="form-group">
            @*@Html.LabelFor(model => model.UserId, new { @class = "col-sm-2 control-label" })*@
            <label class="col-sm-2 control-label"> User ID</label>
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.UserId, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.UserId)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "col-sm-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>        
        <div class="form-group">
            @*@Html.LabelFor(model => model.DoctorSpecialityId, new { @class = "col-sm-2 control-label" })*@
            <label class="col-sm-2 control-label"> Doctor Speciality ID</label>
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.DoctorSpecialityId, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.DoctorSpecialityId)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Charges, new { @class = "col-sm-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Charges, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Charges)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.PhoneNo, new { @class = "col-sm-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.PhoneNo, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.PhoneNo)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.WardId, new { @class = "col-sm-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.WardId, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.WardId)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-primary" id="submit" />
            </div>
        </div>

您没有限制表单提交,因此不会调用ajax成功函数。在单击事件处理程序的末尾添加return false

$('#submit').click(function () {
var modelDataJSON = @Model;
//do ajax call.
return false;
});