asp.net-mvc 如何在不传递参数的情况下重定向到带有其他动作参数的动作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5101919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect to an action with parameters from other action without passing parameters?
提问by learning
Below, in CreateTest, uponsuccessful, I want to redirect to Tests from CreateTest.
下面,在 CreateTest 中,一旦成功,我想从 CreateTest 重定向到测试。
I want to do something like the following:
我想做如下事情:
public ActionResult Tests(int ID, string projectName)
{
TestModel model = new TestModel (ID, projectName);
return View(model);
}
[HttpPost]
public ActionResult CreateTest(TestModel model)
{
try
{
return RedirectToAction("Tests");
}
catch (Exception e)
{
ModelState.AddModelError("Error", e.Message);
return View(model);
}
}
回答by Darin Dimitrov
You might need to provide the arguments when redirecting:
您可能需要在重定向时提供参数:
return RedirectToAction("Tests", new {
ID = model.ID,
projectName = model.ProjectName
});
and the url you will be redirecting to will now look something like this:
你将重定向到的 url 现在看起来像这样:
/Foo/Tests?ID=123&projectName=abc
/Foo/Tests?ID=123&projectName=abc
回答by Peter
I know this is a bit old but...
我知道这有点旧但是......
What I've done in the past is have a "MessageArea" class exposed as a property on my base controller that all my controllers ultimately inherit from. The property actually stores the class instance in TempData. The MessageArea has a method to Add() which takes a string message and an enum Type (e.g. Success, Error, Warning, Information).
我过去所做的是将“MessageArea”类公开为我的基本控制器上的一个属性,我的所有控制器最终都继承自该属性。该属性实际上将类实例存储在 TempData 中。MessageArea 有一个 Add() 方法,它接受一个字符串消息和一个枚举类型(例如成功、错误、警告、信息)。
I then have a partial that renders whatever messages are in MessageArea with appropriate styling according to the type of the message.
然后我有一个部分,根据消息的类型以适当的样式呈现 MessageArea 中的任何消息。
I have a HTMLHelper extension method RenderMessageArea() so in any view I can simple say @Html.RenderMessageArea(), the method and partial take care of nulls and nothing is output if there are no messages.
我有一个 HTMLHelper 扩展方法 RenderMessageArea() 所以在任何视图中我都可以简单地说@Html.RenderMessageArea(),该方法和部分处理空值,如果没有消息,则不输出任何内容。
Because data stored in TempData only survives 1 request it is ideal for cases where you want your action to redirect but have 1 or more messages shown on the destination page, e.g. an error, not authorised page etc... Or if you add an item but then return to the index list page.
因为存储在 TempData 中的数据只能在 1 个请求中存活,所以它非常适合您希望操作重定向但在目标页面上显示 1 个或多个消息的情况,例如错误、未授权页面等......或者如果您添加一个项目然后返回索引列表页面。
Obviously you could implement something similar to pass other data. Ultimately I'd say this is a better solution to the original question than the accepted answer.
显然你可以实现类似的东西来传递其他数据。最终我会说这是对原始问题的更好的解决方案,而不是接受的答案。
EDIT, EXAMPLE:
编辑,示例:
public class MessageAreaModel {
public MessageAreaModel() {
Messages = new List<Message>();
}
public List<Message> Messages { get; private set; }
public static void AddMessage(string text, MessageIcon icon, TempDatadictionary tempData) {
AddMessage(new Message(icon, text), tempData);
}
public static void AddMessage(Message message, TempDataDictionary tempData) {
var msgArea = GetAreaModelOrNew(tempData);
msgArea.Messages.Add(message);
tempData[TempDataKey] = msgArea;
}
private static MessageAreaModel GetAreaModelOrNew(TempDataDictionary tempData) {
return tempData[TempDataKey] as MessageAreaModel ?? new MessageAreaModel();
}
The above class can then be used to add messages from your UI layer used by the controllers.
然后可以使用上述类从控制器使用的 UI 层添加消息。
Then add an HtmlHelper extension like so:
然后像这样添加一个 HtmlHelper 扩展:
public static void RenderMessageArea(this HtmlHelper html) {
html.RenderPartial("MessageArea",
(MessageAreaModel)html.ViewContext.TempData[MessageAreaModel.TempDataKey] ?? MessageAreaModel.Empty);
html.ViewContext.TempData.Remove(MessageAreaModel.TempDataKey);
}
The above is not fully completed code there are various bells and whistles I've left out but you get the impression.
以上不是完全完成的代码,我遗漏了各种花里胡哨的东西,但你会得到印象。
回答by Rob
Make the int
nullable:
使int
可空:
public ActionResult Tests(int? ID, string projectName){
//...
}