0

I'm calling this ActionResult controller method from a JavaScript function on a View. The function works fine, the ActionResult controller method runs fine but the final line where it is supposed to move to a different view does not happen. I can step through the lines of code on the new view but after it steps through the lines on the new view the original view is still displayed. Is the use of the $.get() call correct? Or should I be using some other jquery method to call the controller. I don't need to return anything from the controller in this instance, just run some database code and move to a new view.

Controller:

        if (ModelState.IsValid)
        {
            System.Web.Security.MembershipUser user = Membership.GetUser();
            var job = (from j in db.Jobs where j.ID == id select j).First();
            job.DriverMembershipUserName = user.UserName.ToString();
            job.JobStatus = "Purchased";
            ViewBag.PurchaseMessage = "Thank you for purchasing this job.";
            db.Entry(job).State = EntityState.Modified;
            db.SaveChanges();

            return View("JobDetail", job);                
        }

JavaScript call from original view page:

var url = "/Job/PurchaseJobFinalize/" + JobID;
$.get(url, function () { });
1
  • You are doing an async call, all that it'll return is your rendered html. If you want it to actually go to that "page" use a link instead. Commented Sep 4, 2013 at 8:06

1 Answer 1

1

Consider using

@Html.ActionLink("PurchaseJobFinalize", "Job", new { JobID: JobID});

what you are using now, is an ASync call to the server, and it'll only return a the rendered html of the view.

You can however take the result of the call and put it into the div, that way you are moving into the SPA (single page app) space.

or you can try this

window.location = "/Job/PurchaseJobFinalize/" + JobID;

or you can try

window.location.assign("/Job/PurchaseJobFinalize/" + JobID)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the suggestion. My $.get(url... call is inside a success function so I can't make that an action link. I was hoping the success function could call a controller method, run some code there in the controller, and then move on to the next view.
you can use window.location = "/Job/PurchaseJobFinalize/" + JobID;
ok, thank you. I had just found this and it seems to work also, I think the same idea you have above? location.href = url + "/" + JobID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.