如何在不传递参数的情况下,使用其他动作的参数重定向到一个动作?

时间:2022-05-22 11:12:45

Below, in CreateTest, uponsuccessful, I want to redirect to Tests from CreateTest.

下面,在CreateTest, uponsuccess中,我想从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);
        }
    }

3 个解决方案

#1


29  

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 /测试? ID = 123 projectname = abc

#2


0  

Make the int nullable:

int可以为空:

public ActionResult Tests(int? ID, string projectName){
    //...
}

#3


0  

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(),该方法接受字符串消息和enum类型(例如,Success、Error、Warning、Information)。

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(),这个方法和部分会处理nulls,如果没有消息,则不会输出任何内容。

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.

上面的代码还没有完全完成,我还漏掉了一些花哨的东西,但是你会得到这样的印象。

#1


29  

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 /测试? ID = 123 projectname = abc

#2


0  

Make the int nullable:

int可以为空:

public ActionResult Tests(int? ID, string projectName){
    //...
}

#3


0  

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(),该方法接受字符串消息和enum类型(例如,Success、Error、Warning、Information)。

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(),这个方法和部分会处理nulls,如果没有消息,则不会输出任何内容。

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.

上面的代码还没有完全完成,我还漏掉了一些花哨的东西,但是你会得到这样的印象。