Reputation: 1481
I have a link. When used, I want to load a new view and then, automatically, call an action. But I only know to load views with a "return" within an ActionResult method (so I cannot later call another action!).
Perhaps I am simply thinking about the whole thing wrong, but I cannot figure out how to do this any other way.
One thing I tried is to call the action from the view, but something like @Html.ActionLink will require the user to click, whereas I want the action to be triggered automatically.
Edit:
Since a few asked, here is the motivation for what I am trying to do (feel free to suggest better approaches!):
I want to redirect the user to a view while a long process runs, and then automatically redirect the user to the final view. The second action would call the process and redirect the user when it is complete.
Upvotes: 0
Views: 2770
Reputation: 385
You need to use javascript to check the status of your long running request. Once it has completed you can redirect.
Add an action that reuturns the current status of your operation, e.g.
public JsonResult GetLoadStatus(){
//Get status from somewhere
bool isLoaded = ...
return Json(isLoaded, JsonRequestBehavior.AllowGet);
}
On your page, assuming you have jQuery (haven't tested this):
<div id="LoadStatus">Loading...</div>
<script>
var loadingCompleted = false;
function checkStatus(){
$.get('/MyController/GetLoadStatus')
.done(function(isLoaded){
if(isLoaded){
loadingCompleted = true;
window.location.href = "/resulturl";
}
})
.fail(function(){
alert("Error getting load status");
})
.always(function(){
//Whether the call to your GetLoadStatus method failed or succeeded, always check again in 1 second.
if(loadingCompleted === false){
setTimeout(checkStatus, 1000);
}
});
}
checkStatus();
</script>
Upvotes: 0
Reputation: 149
You can use async ajax call from client to make another request after the view is rendered.
$(document).ready(function(){
jQuery.get( url [, data ] [, success ] [, dataType ] )
});
e.g.
jQuery.get( 'mywebsite/mycontroller/myaction')
Others are optional parameter.
This way you can trigger another action automatically and can handle the reponse of the request in success callback.
$(document).ready(function(){
jQuery.get( 'mywebsite/mycontroller/myaction', {id:1}, onSuccess );
});
function onSuccess(response){
// add logic to do whatever from response
}
Upvotes: 0
Reputation:
I am just guessing what u are trying to do,may be u can try something Like this
TransactionIndex.cshtml
@model List<Transaction>
@Html.Partial("../Transaction/_Transaction",model)
Main.chtml
<a id="transactionLink" href='@Url.Action("GetMainTransactionView","Transaction")'/>
Upvotes: 0
Reputation: 784
You need to get the client to redirect the browser on page load, to automate it add this at the end of the view before the </html>
tag:
<script type="text/javascript">
window.location = "@Url.Action("ActionName", "ControllerName")";
</script>
Although I suspect there better solutions if we know the specific problem you are trying to overcome.
Upvotes: 1