0

i have a button on the cshtml view..its clicked every-time an item is scanned. The user has to do it one by one and once all the items have been scanned..i want to opem/pop up a new window plus redirect him to another page.. The condition whether it was the last item..is being checked in the controller method.
How can i call a javascript to open the new window from the controller..right before my 'redirecttoaction' ?

is there a better way to do it?

2 Answers 2

2

Here's a sample pattern:

public ActionResult Index()
{
    var model = new MyViewModel();
    return View(model);
}

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // TODO Process the scanned code model.Code

    if (IsLastItem())
    {
        model.IsLast = true;
    }
    return View(model);
}

and inside the view:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.TextBoxFor(x => x.Code)
    <input type="submit" value="OK" />
}

<script type="text/javascript">
@if (Model.IsLast)
{
    <text>
    window.open('@Url.Action("foo")', 'foo');
    window.location.href = '@Url.Action("bar")';
    </text>
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

darin.. this is how it is now.. [HttpPost] public ActionResult Index(MyViewModel model) { // TODO Process the scanned code model.Code if (IsLastItem()) { model.IsLast = true; return redirecttoaction("another page"); } return View(model); } As you see, it doesnt post back to the view..but is redirecting to another page.. So basically..i want a pop-up plus redirection.. And so what I am trying is .. call the pop-up right before the redirecttoaction.
0

Its not clean to call JavaScript from controller. Instead, move the logic of checking if its a last item to the client side and call appropriate controller action as appropriate.

4 Comments

mahesh..this will be a javascript window. regardless where the decision is being made..controller action has to call javascript window.open
Then, why not open that window with destination as the redirect action after you check the logic of if its the last element?
there are two actions reqd based on the decision from page-A.. 1. open this pop-up 2. redirect to page B or C. I considered putting this pop-up javascript in the document.ready of subsequent page. But sometimes subsequent page is B..sometimes C thru B.. sometimes directly to C. So.. thats not a solution either
@jeff: I think, you should put the logic that is required to determine which page to got to, from Page X in itself. So that you don't have to worry about it, you just load that page and it knows where to go from there, as an independent entity.

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.