Ajax 调用 POST 操作后未成功执行 ASP.NET MVC
Ajax success not executing after POST action is called ASP.NET MVC
我有这个表格:
@{ using (Html.BeginForm(@*"MakeOffer", "Product", FormMethod.Post, *@new { id = "offerForm", productId = Model.ProductId, sellerId = Model.SellerId }))
{
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
@Html.TextBoxFor(modelItem => modelItem.Price, "{0:#.#}", new { placeholder = "Input your price", id = "offerText", name="offerPrice" })
@Html.HiddenFor(modelItem => modelItem.ProductId)
@Html.HiddenFor(modelItem => modelItem.OffererId)
@Html.HiddenFor(modelItem => modelItem.SellerId)
<input id="offersubmit" type="submit" class="btn" value="Submit" />
}
这是我的 Ajax 函数:
<script>
$(document).ready(function () {
$("#offersubmit").click(function (e) {
if ($(this).valid()true) {
var valdata = $("#offerForm").serialize();
alert(valdata);
$.ajax({
url: "/Product/MakeOffer",
type: "POST",
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: valdata,
success: $(document).ready(function () {
$('#offerText').val('');
})
})
}
});
});
</script>
现在表单正在传递给 MakeOffer
操作,但我收到 500 错误并且未调用成功函数。
这些是我最后的脚本标签:
<script src="~/Scripts/jquery-3.6.0.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
您需要删除成功回调的 $(document).ready(..)
部分。
应该是
success: function(data)
{
$('#offerText').val('');
}
我有这个表格:
@{ using (Html.BeginForm(@*"MakeOffer", "Product", FormMethod.Post, *@new { id = "offerForm", productId = Model.ProductId, sellerId = Model.SellerId }))
{
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
@Html.TextBoxFor(modelItem => modelItem.Price, "{0:#.#}", new { placeholder = "Input your price", id = "offerText", name="offerPrice" })
@Html.HiddenFor(modelItem => modelItem.ProductId)
@Html.HiddenFor(modelItem => modelItem.OffererId)
@Html.HiddenFor(modelItem => modelItem.SellerId)
<input id="offersubmit" type="submit" class="btn" value="Submit" />
}
这是我的 Ajax 函数:
<script>
$(document).ready(function () {
$("#offersubmit").click(function (e) {
if ($(this).valid()true) {
var valdata = $("#offerForm").serialize();
alert(valdata);
$.ajax({
url: "/Product/MakeOffer",
type: "POST",
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: valdata,
success: $(document).ready(function () {
$('#offerText').val('');
})
})
}
});
});
</script>
现在表单正在传递给 MakeOffer
操作,但我收到 500 错误并且未调用成功函数。
这些是我最后的脚本标签:
<script src="~/Scripts/jquery-3.6.0.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
您需要删除成功回调的 $(document).ready(..)
部分。
应该是
success: function(data)
{
$('#offerText').val('');
}