I`m trying to send a form in mvc with Ajax.beginform and execute on success function. The function is stated in the form and created in the scripts section. However, the function is not fired after the post is submitted.

The view is partial view and is inserted in Index page.

The method which the form calls in the controller is HttpPost and returns the view again which causes the index page to load again.( I suspect here is the problem)

Basically what im trying to achieve is to show a div in my index page(main) saying successfully record edited.

Also, in my bundle config I have the jquery val & the jquery.validate.unobtrusive.js loaded and in the web.config I added in appconfig

What could be wrong? Here is my code.

   [HttpPost]
    public async Task<ActionResult> EditPostedJob(JobPost job, string dt)
    {

        using (var context = new ApplicationDbContext())
        {
            var post = (from p in context.jobPosts where p.JobPostId == job.JobPostId select p).First();
            post.AboutJob = job.AboutJob;
            post.Headline = job.Headline;
            post.JobAddress = job.JobAddress;
            post.JobCity = job.JobCity;
            post.JobPostCode = job.JobPostCode;
            await context.SaveChangesAsync();

            //return Json(post,JsonRequestBehavior.AllowGet);
            return RedirectToAction("Index", "Manage");

        }

    }

in my main view I have this message

 <div id="success-edited-job" class="alert alert-success">
            <strong>Success!</strong> the posted job has been         successfully edited.
        </div>

and also I have the js which hides this when page is loaded. And then I add the partial view with the form and there is set the success-edited-job div to show when post is successful but it return again the index view and the div is hidden again.

this is my partialview form

   @using (Ajax.BeginForm("EditPostedJob", FormMethod.Post, new     AjaxOptions() { OnSuccess = "JobEditedSuccessfully", OnFailure = "JobEditFail",HttpMethod="POST" }))

and the js

   <script type="text/javascript">


    function JobEditedSuccessfully(data) {
        alert("lol");


    }

    function JobEditFail(error) {
        console.error(error);
    }

   </script>

0 Answers