Javascript 在MVC4中使用Ajax调用动作方法

Javascript 在MVC4中使用Ajax调用动作方法,javascript,ajax,jquery,asp.net-mvc-4,Javascript,Ajax,Jquery,Asp.net Mvc 4,动作方法在我的代码中不触发。你能告诉我错误吗 这是密码 <script type="text/javascript"> $("#btnSave").click(function () { var ContactID = $("#txtContactId").val(); var Company = $("#txtCompany").val(); var Status = $("#cmbStatus").val(); var IsActive = $

动作方法在我的代码中不触发。你能告诉我错误吗

这是密码

<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");
        }
    });
});

这可能是一个错误的url问题。使用
Url.Action()
helper:

$.ajax({
        url: "@Url.Action("InsertCustomer", "Customer")",
您还可以检查浏览器控制台以了解错误详细信息

顺便说一句,如果要从表单发送值,可以使用jquery方法:


您需要设置[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);
    }

我试过你表现出来的方式。但它仍然不起作用。谢谢。:)我发现了错误。操作方法需要[HttpPost]属性。感谢您的帮助:)------------------------------------------------------------[HttpPost]公共操作结果InsertCustomer(字符串…哦,我看到您自己得到了:)
$.ajax({
            url: "@Url.Action("InsertCustomer", "Customer")",
            data: $('form').serializeArray()
[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);
    }