尝试在不重新加载页面(MVC)的情况下将错误消息从Controller传递到View [英] Trying to pass error message from Controller to View without page reload (MVC)

查看:66
本文介绍了尝试在不重新加载页面(MVC)的情况下将错误消息从Controller传递到View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的控制器的ActionResult失败时,我试图获取我的视图以显示错误消息. (当您按下视图"中的按钮时触发.)

I'm trying to get my View to display an error message when the ActionResult from my controller fails. (It's triggered when you press a button in the View.)

但是,无论我如何尝试,ActionResult始终都会重定向到另一个页面.如果我尝试返回View(),它将转到默认的黄色ASP.Net错误页面.如果我尝试返回Content(错误消息"),它只会打开包含该文本的空白页.

However, no matter what I try, the ActionResult always redirects to a different page. If I try to return View() it goes to the default yellow ASP.Net error page. If I try to return Content("Error message") it just opens a blank page with that text.

我已将罐装错误消息保存在ViewBag.Error中,但是视图中的javascript永远没有机会使用它,因为一旦您单击我的按钮,ActionResult就会将我重定向到某个地方.

I've saved my canned error message in ViewBag.Error, but the javascript in my View never gets a chance to use it because as soon as you click my button, the ActionResult redirects me somewhere.

如何使View保持原样,但如果ActionResult失败,仍然传递消息?

How do I get my View to stay put, but still pass a message if the ActionResult fails?

这是我在控制器中调用的方法:

Here is the method I'm calling in the controller:

public ActionResult ElectrodeChecklist(int id)
{
    var routeChecklist = _routeService.GetRouteChecklist(id);

    // This is where I'm trying to catch this error.
    if (routeChecklist.Count == 0)
    {
        ViewBag.Error = "This route has no checklist steps to print.";
        return ???
    }

    ...
    blah blah stuff to download a PDF file when everything works
    ...


    return new BinaryContentResult(buffer, "application/pdf", filename);
}

这是视图:

... dropdown menu called #dd-route...

<a href="#" id="btn-print" class="btn btn-success btn-sm">Generate PDF</a>

<script type="text/javascript">

    $(function () {

        $('#dd-route').change(function () {
            var $route = $(this).val();
            var $url = '@Url.Action("electrodechecklist", "wip")?id=' + $route;
            $('#btn-print').attr('href', $url);
        });

        $('#btn-print').click(function () {
            var $error = ViewBag.Error;
            if ($error != "") {
                $('#alertbar').show();
                $('#alert').html($error);
            }
        })

    });


</script>

当用户在下拉菜单中选择一个项目时,用于调用我的ActionResult的网址会输入到按钮的href属性中.然后,当他们单击该按钮时,它将使用下拉菜单中的正确值将其触发.

When the user chooses an item in the dropdown menu, the url to call my ActionResult is fed into the button's href attribute. Then when they click the button, it fires it off with the correct value from the dropdown.

JavaScript的第二部分是我尝试显示错误消息(如果它们在Controller中命中了If语句).

The second part of the JavaScript is my attempt at displaying the error message if they hit that If statement in the Controller.

推荐答案

我之所以写这篇文章是因为它不适合注释.

I'm only writing this because it won't fit comments.

好的,所以首先要注意的是它将开始使用id导航到操作ElectrodeChecklist,然后执行BinaryContentResult所做的任何事情.我看不到var $error = ViewBag.Error;可能如何工作,但是我又不确定BinaryContentResult的实际功能.

OK so first thing to notice is that it WILL start navigating to the Action ElectrodeChecklist with the id and then do what ever BinaryContentResult does. I cannot see how var $error = ViewBag.Error; could possibly work, but then again I'm not sure how/what BinaryContentResult really does.

如果您希望用户看到ViewBag.Error,则需要修改此部分

If you want the user to see the ViewBag.Error then you need to modify this part

if (routeChecklist.Count == 0)
{
    TempData["Error"]  = "This route has no checklist steps to print.";
    return  RedirectToAction("ACTION", "CONTROLLER");
}

由@StephenMuecke建议. ACTION是用户最初所在的页面.您可以将它们重定向到任何错误页面,然后仍然访问TempData["Error"].如果这样做仍然无法解决您想要的事情,请告诉我们.

as suggested by @StephenMuecke. The ACTION here is the page the user was on in the first place. You could redirect them to any error page and still access the TempData["Error"]. If this still doesn't do what you want let us know.

 //What you are trying to do here is awesome 
 //but I'm not sure if it's even possible as you are moving away from the page when the user clicks the button/link
 //I'd like other please comment on this. I could use it too
$('#btn-print').click(function () {
        var $error = ViewBag.Error;
        if ($error != "") {
            $('#alertbar').show();
            $('#alert').html($error);
        }
    })

这篇关于尝试在不重新加载页面(MVC)的情况下将错误消息从Controller传递到View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆