[英]Ajax POST jQuery issue
我正在尝试从数据库中删除一个元素....我在我的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>
您不限制表单提交,因此不会调用ajax成功函数。 在click事件处理程序的末尾添加return false
。
$('#submit').click(function () {
var modelDataJSON = @Model;
//do ajax call.
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.