ASP.NET MVC - 通过传递一些数据重定向到一个控制器/动作 [英] ASP.NET MVC - redirecting to a Controller/Action by passing some data

查看:323
本文介绍了ASP.NET MVC - 通过传递一些数据重定向到一个控制器/动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在需要检查执行前的一些条件不同的控制器动作。如果条件不满足,我希望用户被重定向到另一个页面上下一步该做什么指令(指令将包括一个链接,用户必须遵守)。

I have actions in different controllers that need to check for a some condition before execution. If the condition is not met, I want the user be redirected to another page with instructions on what to do next (the instructions will include a link that the user must follow).

例如SendMessage函数()动作位于的消息控制器:

For example SendMessage() action is located in the Message controller:

public ActionResult SendMessage()
{
    // check if user has enough credit
    if (!hasEnoughCredit(currentUser))
    {
    	// redirect to another page that says:

    	// "You do not have enough credit. Please go to LinkToAddCreditAction
    	// to add more credit."
    }

    // do the send message stuff here 
}

我想有一个通用的行动称为ShowRequirements()位于要求控制器。

I want to have a single generic action called ShowRequirements() located in Requirements controller.

在SendMessage函数()动作,我想设置,我想展示给用户的消息,然后转发用户ShowRequirements()动作。我只是不希望消息出现在ShowRequirements行动的URL。

In SendMessage() action, I would like to set the message that I want to show to the user and then forward the user to ShowRequirements() action. I just don't want the message to appear in the URL of the ShowRequirements action.

有什么办法)进行通信这些数据来ShowRequirements(动作?

Is there any way to communicate this data to ShowRequirements() action?

推荐答案

好吧,我觉得我得到它错了。正如约翰和安德鲁提到我只需要通过ViewData的数据传递到视图。

Okay, I think I was getting it wrong. As John and Andrew mentioned I simply have to pass the data via ViewData to a view.

于是我在共享的/视图/做出了RequirementsPage.aspx。现在我任何行动,我填写ViewData字典,并把它传递给RequirementsPage.aspx是这样的:

So I made a RequirementsPage.aspx in the /views/Shared. Now in whichever action I am, I fill in the ViewData dictionary and pass it to the RequirementsPage.aspx like this:

public ActionResult SendMessage()
{
    // check if user has enough credit
    if (!hasEnoughCredit(currentUser))
    {
        // redirect to another page that says:
        ViewData["key1"] = "some message";
        ViewData["key2"] = "UrlToTheAction";
        return View("RequirementsPage");
    }

    // do the send message stuff here 
}

这篇关于ASP.NET MVC - 通过传递一些数据重定向到一个控制器/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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