C# 从 cshtml 页面重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17653736/
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
Redirecting from cshtml page
提问by Jed I
I want to redirect to a different view depending on the result of a dataset, but I keep getting returned to the page I am currently on, and can't work out why. I drop into the if statement the action gets called but once i return the view to the new page, it returns me back to the current page.
我想根据数据集的结果重定向到不同的视图,但我一直返回到我当前所在的页面,无法弄清楚原因。我进入 if 语句,该操作被调用,但是一旦我将视图返回到新页面,它就会将我返回到当前页面。
CSHTML page
CSHTML 页面
@{
ViewBag.Title = "Search Results";
EnumerableRowCollection<DataRow> custs = ViewBag.Customers;
bool anyRows = custs.Any();
if(anyRows == false)
{
Html.Action("NoResults","Home");
}
// redirect to no search results view
}
}
Controller
控制器
public ActionResult NoResults()
{
return View("NoResults");
}
View I cant get too..
查看我也无法获得..
@{
ViewBag.Title = "NoResults";
}
<h2>NoResults</h2>
采纳答案by Mostafa Soghandi
Change to this:
改成这样:
@{ Response.Redirect("~/HOME/NoResults");}
回答by Madushan
Would be safer to do this.
这样做会更安全。
@{ Response.Redirect("~/Account/LogIn?returnUrl=Products");}
@{ Response.Redirect("~/Account/LogIn?returnUrl=Products");}
So the controller for that action runs as well, to populate any model the view needs.
因此,该操作的控制器也会运行,以填充视图所需的任何模型。
Although as @Satpal mentioned, I do recommend you do the redirecting on your controller.
尽管正如@Satpal 所提到的,我确实建议您在控制器上进行重定向。
回答by Gecko IT
This clearly is a bad case of controller logic in a view. It would be better to do this in a controller and return the desired view.
这显然是视图中控制器逻辑的不良情况。最好在控制器中执行此操作并返回所需的视图。
[ChildActionOnly]
public ActionResult Results()
{
EnumerableRowCollection<DataRow> custs = ViewBag.Customers;
bool anyRows = custs.Any();
if(anyRows == false)
{
return View("NoResults");
}
else
{
return View("OtherView");
}
}
Modify NoResults.cshtml to a Partial.
将 NoResults.cshtml 修改为 Partial。
And call this as a Partial view in the parent view
并将其称为父视图中的局部视图
@Html.Partial("Results")
You might have to pass the Customer collection as a model to the Result action or in a ViewDataDictionary due to reasons explained here: Can't access ViewBag in a partial view in ASP.NET MVC3
由于此处解释的原因,您可能必须将 Customer 集合作为模型传递给 Result 操作或在 ViewDataDictionary 中:无法在 ASP.NET MVC3 中的部分视图中访问 ViewBag
The ChildActionOnlyattribute will make sure you cannot go to this page by navigating and that this view must be rendered as a partial, thus by a parent view. cfr: Using ChildActionOnly in MVC
该ChildActionOnly属性将确保您无法通过导航进入此页面,并且此视图必须呈现为局部视图,因此由父视图呈现。cfr:在 MVC 中使用 ChildActionOnly
回答by sachin singh
You can go to method of same controller..using this line , and if you want to pass some parameters to that action it can be done by writing inside ( new { } ).. Note:- you can add as many parameter as required.
您可以转到同一控制器的方法..使用这一行,如果您想将一些参数传递给该操作,可以通过在内部写入( new { } )来完成.. 注意:-您可以根据需要添加任意数量的参数.
@Html.ActionLink("MethodName", new { parameter = Model.parameter })
@Html.ActionLink("MethodName", new { parameter = Model.parameter })

