Ajax POST jQuery 问题
Ajax POST jQuery issue
我正在尝试从数据库中删除一个元素....我已经在我的 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);
}
我需要看看它是否被删除,并通知用户它是否已成功删除......但是问题是当我 return 这是 Json 字符串不会移动到我的 jQuery POST 函数,它只会在浏览器中显示我的字符串...................... …………
仅作为示例,它只会输出此消息:"You have successfully Removed your record!!".......
这就是我的功能的样子:
$('#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;
});
我正在尝试从数据库中删除一个元素....我已经在我的 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);
}
我需要看看它是否被删除,并通知用户它是否已成功删除......但是问题是当我 return 这是 Json 字符串不会移动到我的 jQuery POST 函数,它只会在浏览器中显示我的字符串...................... ………… 仅作为示例,它只会输出此消息:"You have successfully Removed your record!!"....... 这就是我的功能的样子:
$('#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;
});