Question

I've been trying to make the "RedirecttoAction" statement work here but it doesn't budge. Here is the method:

    [HttpPost]
    public ActionResult UpdateTech(int tech, int id)
    {
        Callout callout = db.Callouts.Find(id);
        callout.TechnicianId = tech;
        callout.TechStatus = "ASSIGNED";
        db.Entry(callout).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("CalloutAdmin/Index/");
    }

And it is called by the following JQuery, which works perfectly:

$(function () {
        $(".techdropdown").change(function () {
            var recordToUpdate = $(this).attr("id");
            var selectedTech = $(".techdropdown option:selected").val();
            window.alert(selectedTech);
            $.post("/CalloutAdmin/UpdateTech?tech=" + selectedTech + "&id=" + recordToUpdate);
        });
    });

The only thing that does not happen is the page being refreshed, which was what the redirect should do. Can anyone advise?

UPDATE

What I am trying to do is update this Callout object, without redirecting to another view. I choose a technician from a dropdownlist and it automatically sets the callout's technician value to the one I chose from the dropdown.

Was it helpful?

Solution

You're returning the redirect to the AJAX request, and the AJAX request is loading the page in the background. If you want to have the entire page redirect you probably want to use the success callback in jQuery

$.post("/CalloutAdmin/UpdateTech?tech=" + selectedTech + "&id=" + recordToUpdate, function() { document.location = "CalloutAdmin/Index/"; } );

If you want to have the action control the location of the redirect, you probably want to use the return a JsonResult or ContentResult from your action with the URL outputted from there have jQuery's success callback redirect the user.

OTHER TIPS

Assuming that UpdateTech and Index both belong to CallOutAdminController then change your UpdateTech Method to look like:

[HttpPost]
public ActionResult UpdateTech(int tech, int id)
{
    Callout callout = db.Callouts.Find(id);
    callout.TechnicianId = tech;
    callout.TechStatus = "ASSIGNED";
    db.Entry(callout).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index","CallOutAdmin");
}

Then make you jQuery function like this:

$(document).ready(function(e) {
    $(".techdropdown").change(function () {
        var recordToUpdate = $(this).attr("id");
        var selectedTech = $(".techdropdown option:selected").val();
        window.alert(selectedTech);
        window.location = "/CallOutAdmin/UpdateTech?tech=" + selectedTech + "&id=" + recordToUpdate
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top