简体   繁体   English

在MVC4中使用Ajax调用Action方法

[英]Call an Action method using Ajax in MVC4

Action method doesn't fire in my code. Action方法不会在我的代码中触发。 Can you please show me the error. 你能告诉我错误吗? :) :)

here is the code.. 这是代码..

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

Here is the Action method.. 这是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);
    }

You need to set [HttpPost] attribute: 您需要设置[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);
    }

It can be an issue with the wrong url. 这可能是错误网址的问题。 Use Url.Action() helper: 使用Url.Action()帮助器:

$.ajax({
        url: "@Url.Action("InsertCustomer", "Customer")",

Also you can check your browser console for error details. 您还可以检查浏览器控制台以获取错误详细信息。

Btw, if you want to send values from the form you can use the jquery .serializeArray() method: 顺便说一句,如果你想从表单发送值,你可以使用jquery .serializeArray()方法:

$.ajax({
            url: "@Url.Action("InsertCustomer", "Customer")",
            data: $('form').serializeArray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM