最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call an Action method using Ajax in MVC4 - Stack Overflow

programmeradmin4浏览0评论

Action method doesn't fire in my code. 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..

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

Action method doesn't fire in my code. 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..

        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);
    }
Share Improve this question edited Aug 8, 2013 at 10:15 bipen 36.6k9 gold badges50 silver badges62 bronze badges asked Aug 8, 2013 at 10:14 tishanthatishantha 4975 gold badges13 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You need to set [HttpPost] attribute:

[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:

$.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:

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

评论列表(0)

  1. 暂无评论