gpt4 book ai didi

javascript - 在 MVC4 中使用 Ajax 调用 Action 方法

转载 作者:行者123 更新时间:2023-11-30 12:58:42 24 4
gpt4 key购买 nike

我的代码中没有触发操作方法。你能告诉我错误吗? :)

这是代码..

<script type="text/javascript">

$("#btnSave").click(function () {

var ContactID = $("#txtContactId").val();
var Company = $("#txtCompany").val();
var Status = $("#cmbStatus").val();
var IsActive = $("#IsActive").is(':checked');
var Comments = $("#txaComments").val();

var Country = $("#cmbCountry").val();
var Address1 = $("#txtAddress1").val();
var Address2 = $("#txtAddress2").val();
var City = $("#txtCity").val();
var State = $("#txtState").val();

var PostalCode = $("#txtPostalCode").val();
var VatNo = $("#txtVatNo").val();
var RegNo = $("#txtRegNo").val();
var Phone = $("#txtPhone").val();
var Email = $("#txtEmail").val();

$.ajax({
url: "Customer/InsertCustomer",
data: {
'ContactID': ContactID,
'Company': Company,
'Status': Status,
'IsActive': IsActive,
'Comments': Comments,
'Country': Country,
'Address1': Address1,
'Address2': Address2,
'City': City,
'State': State,
'PostalCode': PostalCode,
'VatNo': VatNo,
'RegNo': RegNo,
'Phone': Phone,
'Email': Email
},
dataType: "json",
type: 'POST',
success: function (data) {
alert("Successfully Inserted!");
},
error: function () {
alert("error");
}
});
});

这是 Action 方法..

        public ActionResult InsertCustomer(string ContactID, string Company, int Status, bool IsActive, string Comments, int Country, string Address1, string Address2, string City, string State, string PostalCode, string VatNo, string RegNo, string Phone, string Email)
{
bool process = false;

return Json(process, JsonRequestBehavior.AllowGet);
}

最佳答案

需要设置[HttpPost]属性:

[HttpPost]
public ActionResult InsertCustomer(string ContactID, string Company, int Status, bool IsActive, string Comments, int Country, string Address1, string Address2, string City, string State, string PostalCode, string VatNo, string RegNo, string Phone, string Email)
{
bool process = false;

return Json(process, JsonRequestBehavior.AllowGet);
}

关于javascript - 在 MVC4 中使用 Ajax 调用 Action 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18123381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com