1
  1. 我有一条 ViewBag 字符串消息。此消息从操作链接传递到控制方法。然后可以在基于 viewbag 消息的操作方法中使用一些逻辑。

  2. 同样的动作结果可以设置view bag消息,这样当一个view page被加载时,jquery会比较viewbag消息,如果有任何匹配,就会显示一个toast消息。

  3. 这要求我可以控制程序的流程。所以我知道哪种方法将处理actionlink。

  4. 问题是,使用实体框架的用户身份。如果该方法被授权阻止,则该方法将自动绕过,并向用户呈现登录页面。

  5. 这意味着,我的剃须刀没有将任何信息传递给该特定方法。

ActionLink 有 viewbag 消息 -> 作为参数传递给控制器
​​ Controller 设置消息 -> 加载页面时,检查消息。

我一直在将 string ViewBag.Message视图传递给控制器​​,toastr用于在整个项目中创建弹出窗口。它运作良好。

我有一个项目,当用户想要签出时使用[Authorize]内置的。UserIdentity

[Authorize]
public ActionResult Checkout(string message)
{

问题是,消息永远不会传递给LoginAction方法。

[AllowAnonymous]
public ActionResult Login(string message,string returnUrl)
{

我尝试在视图中创建一条消息。

@{
    string message = ViewBag.Message;
}

/.../

@Html.ActionLink("Go To Check Out", "Checkout", "Carts", 
new {message = "You Need to Login to Checkout!" })

我已经尝试确定是否可以确定使用哪个链接来调用登录视图。我已经断断续续地研究了一段时间。
我无法弄清楚如何做到这一点,我认为如果我知道如何做到这一点会很好。
任何帮助表示赞赏。

编辑

这个怎么运作:

[HttpPost]
    public ActionResult RemoveFromCart(int prod_id)
    {
        Cart cart = (Cart)Session["Cart"];
        cart.RemoveFromCart(prod_id);
        return RedirectToAction("Index", new { message = "Cart Updated!" });
    }

使用索引:

@section Scripts {
    <script src="~/Scripts/jquery-2.1.4.js"></script>
    <link href="~/Content/toastr.css" rel="stylesheet" />
    <script src="~/Scripts/toastr.js"></script>
    <script src="~/Scripts/toastr.min.js"></script>
    <script type="text/javascript">

    $(document).ready(function () {
        if ('@ViewBag.Message' == "Cart Updated!") {
            toastr.options.timeOut = 1500; // 1.5s
            toastr.success('@ViewBag.Message');
        }
        if ('@ViewBag.Message' == "Login Successful!") {
            toastr.options.timeOut = 1500; // 1.5s
            toastr.success('@ViewBag.Message');
        }
        if ('@ViewBag.Message' == "Registration Successful!") {
            toastr.options.timeOut = 1500; // 1.5s
            toastr.success('@ViewBag.Message');
        }
    });
</script>
}

所以实际上 viewbag 消息是在控制器中设置的,然后在页面加载时比较消息(在 jquery 中)。
所以我正试图从头到尾做,因为我在想办法做到这一点时已经束手无策了。

所以我想检查帐户/登录控制器我需要检查登录是否来自试图在没有登录的情况下结帐的用户,因为它只是使用 [Authorize] 安全检查重定向到那里,我不知道该怎么做。

    [AllowAnonymous]
    public ActionResult Login(string message,string returnUrl)
    {
        if (message == null)
        {
            message = "";
        }

   // Algorithm - IF this was redirected from an attempt to checkout without being logged in. Have the view bag message =="You Need to Login to Checkout!"
        

        ViewBag.ReturnUrl = returnUrl;
        
        return View();
    }

这样,当登录页面加载时,将会有一条 toast 消息,因为在加载每个页面时都会检查该消息。

编辑 402,128,841,442,222 但仍然不是 wiki ;)

该问题由以下解释。

ViewBag 和 ViewData 的相似之处:

当您从控制器移动到视图时,有助于维护数据。
用于将数据从控制器传递到相应的视图。
寿命短意味着发生重定向时值变为空。
这是因为他们的目标是提供一种在控制器和视图之间进行通信的方式。它是服务器调用中的一种通信机制。

什么是 ViewData、ViewBag 和 TempData?– 在当前请求和后续请求之间传递数据的 MVC 选项

所以我想我需要找到另一种方法来做到这一点,欢迎任何想法。

4

2 回答 2

1

如果您尝试将属性作为 GET 参数传递给您的操作,那么您使用的是错误版本的@Html.ActionLink.

这种方法有大约一百万个覆盖。您正在传递new message = { "blah" }HtmlAttributes- 这些将<a>作为 html 属性在标签上可见。您需要使用此覆盖:

@Html.ActionLink("Go To Check Out", "Checkout", "Carts", new { @class ="cssClass-for-link"}, new { message ="You Need to Login to Checkout!"})
于 2015-09-02T08:27:00.163 回答
0

我通过删除注释解决了这个问题,[Authorize]并检查了用户是否已登录,如果用户未登录,则使用字符串消息重定向到登录页面。

// [Authorize]
public ActionResult Checkout(string message)
{
    if(message==null)
    {
        message = "";
    }
    ViewBag.Message = message;

    if (!User.Identity.IsAuthenticated) {
        return RedirectToAction("Login","Account", new { message = "You Need to Login to Checkout!" });
    }
    /.../
于 2015-09-04T09:02:17.650 回答