I have a form which can contain files and it shows a progress bar successfully. My problem is that in the MVC Controller I have:
return RedirectToAction("Complete", new { title = title });
This goes and executes the Complete method if I step through but it then goes back to the JavaScript load event and does not redirect me unless I add a JavaScript redirect in the load event listener (uploadComplete):
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "/Submit-Recipe", true);
xhr.send(fd);
function uploadComplete(evt) {
//var valueFromController = how do I get a value here?;
//window.location.replace("http://@ConfigurationManager.AppSettings["website"]/SubmitRecipeComplete/" + valueFromTheController);
}
Is there a way to not have to add a JavaScript redirect? If I do need a JavaScript redirect, how can I pass a variable back from the controller when it needs to be an ActionResult (for if there is an error and I need to redisplay the page?).
I can work around this by adding a second page where I could know the “valueFromController” but 2 forms breaks the workflow a bit.
EDIT
I did what Praveen said but GetData() did not work for me so I used this instead:
evt.currentTarget.responseText
To determine if to return the View or Content I added this just before xhr.send(fd);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
In the controller I can then do:
if (Request.IsAjaxRequest())
{
return Content(title);
}
1 Answer