Click here to Skip to main content
15,888,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There, can someone pls help me here, i am calling below ajax function on button click, this function is calls the controller method for 1st time , 2nd time and on 3rd time function is not calling controller method,without calling controller method it executes sucess block. i have tried adding asc:false in ajax options that dint work.
JavaScript
function Applicant() {
    $.ajax({
        type: "GET",
        url: "/CreateApplicationFromSearch/CreateApplicationFromSearch",
        contentType: "application/json; charset=utf-8",
        async: false,
        data: { "Applicant_id": ApplicantId, "Application_id": ApplicationId, "OfficeHolder_id": OfficeHolderId, "AccountDetail_Id": AccountDetailId, "contact_person_id": ContactPersonId },
        dataType: "html",
        success: function (data) {

            ("Sucess");
        },

        error: function () {
            alert("Errors arose.");
        }
    });

@using (Html.BeginForm("SaveApplicant", "CreateApplicationFromSearch", FormMethod.Post, new { Application_id = @ViewBag.Application_id, OfficeHolder_id = @ViewBag.OfficeHolder_id, enctype = "multipart/form-data", id = "ApplicantForm" }))


Code in Controller
C#
[httpGet]
[AllowAnonymous]

public ActionResult CreateApplicationFromSearch(int Applicant_id, int Application_id, string OfficeHolder_id, int? AccountDetail_Id, string contact_person_id, bool? CameFromLink, bool? Application_Submitted,bool? Validate_PartOne=false )

{
Applicant = Applicant_Service.GetApplicantById(Applicant_id);
@ViewBag.Application_id = Application_id;
ViewData["OfficeHolder_id"] = OfficeHolder_id;
ViewData["AccountDetail_Id"] = AccountDetail_Id;
ViewData["contact_person_id"] = contact_person_id;
return View("../Application/CreateApplicantFromSearch", Applicant);

}


What I have tried:

tried adding async: false in ajax options.
Posted
Updated 3-Aug-17 0:14am
v3
Comments
Richard Deeming 1-Aug-17 14:24pm    
The jQuery ajax method doesn't accept an option called "asc". Did you mean "cache" instead?

jQuery.ajax() | jQuery API Documentation[^]
Member 12697982 1-Aug-17 16:27pm    
Sorry i have tried async: false, when you need that ajax request to be completed before the browser passes to other code
Richard Deeming 2-Aug-17 7:24am    
You should avoid using the async flag, since it causes the browser to hang whilst the request is being processed.

Try using cache:false to see if the response is being cached.
Member 12697982 2-Aug-17 20:51pm    
Hi Thanks (cache:false) did work

1 solution

As discussed in the comments, it's a caching issue. Adding cache:false to the AJAX call will work around the problem.
JavaScript
$.ajax({
    type: "GET",
    url: "/CreateApplicationFromSearch/CreateApplicationFromSearch",
    contentType: "application/json; charset=utf-8",
    cache: false, // <-- Prevent the request from being fulfilled from the cache
    ...

jQuery.ajax() | jQuery API Documentation[^]

Other options would include using a custom attribute on the action to disable browser caching:
c# - Prevent Caching in ASP.NET MVC for specific actions using an attribute - Stack Overflow[^]
C#
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}

...

[HttpGet]
[NoCache]
[AllowAnonymous]
public ActionResult CreateApplicationFromSearch(...)

Or changing the action to use a POST request, which will never be cached.
C#
[HttpPost]
[AllowAnonymous]
public ActionResult CreateApplicationFromSearch(...)
JavaScript
$.ajax({
    type: "POST",
    url: "/CreateApplicationFromSearch/CreateApplicationFromSearch",
    contentType: "application/json; charset=utf-8",
    ...

Some HTTP methods MUST cause a cache to invalidate an entity. ... These methods are:
  • PUT
  • DELETE
  • POST
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900