ASP。Net MVC重定向到另一个视图

时间:2022-03-27 17:59:05

Is it possible to redirect to a different view from a controller?

是否可以重定向到与控制器不同的视图?

For example, all my controllers inherit from a custom controller that has a constructor that I want to redirect to different view if certain criteria is not met. Hope that makes sense.

例如,我的所有控制器都继承自一个自定义控制器,该控制器具有一个构造函数,如果不满足某些条件,我想将其重定向到不同的视图。希望是有意义的。

5 个解决方案

#1


121  

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is:

您可以使用RedirectToAction()方法,然后您重定向的操作可以返回一个视图。最简单的方法是:

return RedirectToAction("Index", model);

Then in your Index method, return the view you want.

然后在索引方法中,返回所需的视图。

#2


16  

 if (true)
 {
   return View();
 }
 else
 {
   return View("another view name");
 }

#3


3  

I am not 100% sure what the conditions are for this, but for me the above didn't work directly, thought it got close. I think it was because I needed "id" for my view by in the model it was called "ObjectID".

我不是百分之百的确定这个条件是什么,但是对我来说,上面的方法并没有直接起作用,我认为它已经很接近了。我认为这是因为我在模型中需要"id"它被称为" objective "

I had a model with a variety of pieces of information. I just needed the id.

我有一个模型,里面有各种各样的信息。我只是需要身份证。

Before the above I created a new System.Web.Routing.RouteValueDictionary object and added the needed id.

在上面之前,我创建了一个新的System.Web.Routing。RouteValueDictionary对象并添加所需的id。

(System.Web.Routing.)RouteValueDictionary RouteInfo = new RouteValueDictionary();
RouteInfo.Add("id", ObjectID);
return RedirectToAction("details", RouteInfo);

(Note: the MVC project in question I didn't create, so I don't know where all the right "fiddly" bits are.)

(注意:我没有创建的MVC项目,所以我不知道正确的“fiddly”位在哪里。)

#4


0  

Here's what you can do:

你可以这样做:

return View("another view name", anotherviewmodel);

#5


0  

The simplest way is use return View.

最简单的方法是使用返回视图。

return View("ViewName");

Remember, the physical name of the "ViewName" should be something like ViewName.cshtml in your project, if your are using MVC C# / .NET.

记住,“ViewName”的物理名称应该类似于ViewName。如果您正在使用MVC c# /。net,则在您的项目中使用cshtml。

#1


121  

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is:

您可以使用RedirectToAction()方法,然后您重定向的操作可以返回一个视图。最简单的方法是:

return RedirectToAction("Index", model);

Then in your Index method, return the view you want.

然后在索引方法中,返回所需的视图。

#2


16  

 if (true)
 {
   return View();
 }
 else
 {
   return View("another view name");
 }

#3


3  

I am not 100% sure what the conditions are for this, but for me the above didn't work directly, thought it got close. I think it was because I needed "id" for my view by in the model it was called "ObjectID".

我不是百分之百的确定这个条件是什么,但是对我来说,上面的方法并没有直接起作用,我认为它已经很接近了。我认为这是因为我在模型中需要"id"它被称为" objective "

I had a model with a variety of pieces of information. I just needed the id.

我有一个模型,里面有各种各样的信息。我只是需要身份证。

Before the above I created a new System.Web.Routing.RouteValueDictionary object and added the needed id.

在上面之前,我创建了一个新的System.Web.Routing。RouteValueDictionary对象并添加所需的id。

(System.Web.Routing.)RouteValueDictionary RouteInfo = new RouteValueDictionary();
RouteInfo.Add("id", ObjectID);
return RedirectToAction("details", RouteInfo);

(Note: the MVC project in question I didn't create, so I don't know where all the right "fiddly" bits are.)

(注意:我没有创建的MVC项目,所以我不知道正确的“fiddly”位在哪里。)

#4


0  

Here's what you can do:

你可以这样做:

return View("another view name", anotherviewmodel);

#5


0  

The simplest way is use return View.

最简单的方法是使用返回视图。

return View("ViewName");

Remember, the physical name of the "ViewName" should be something like ViewName.cshtml in your project, if your are using MVC C# / .NET.

记住,“ViewName”的物理名称应该类似于ViewName。如果您正在使用MVC c# /。net,则在您的项目中使用cshtml。