Ajax POST jQuery问题

时间:2015-02-24 11:29:02

标签: javascript jquery ajax jquery-mobile

我正在尝试从数据库中删除一个元素....我在我的MVC控制器中创建了这个Action方法.......

    [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>

1 个答案:

答案 0 :(得分:0)

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

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

var modelDataJSON = @Model;

//do ajax call.

return false;
});