开发者

call javascript method from mvc3 controller?

开发者 https://www.devze.com 2023-02-19 20:01 出处:网络
i have a button on t开发者_高级运维he 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 win

i have a button on t开发者_高级运维he 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?


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消