简体   繁体   English

从cshtml页面重定向

[英]Redirecting from cshtml page

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>

改为:

@{ Response.Redirect("~/HOME/NoResults");}

Would be safer to do this. 这样做会更安全。

@{ Response.Redirect("~/Account/LogIn?returnUrl=Products");}

So the controller for that action runs as well, to populate any model the view needs. 因此,该操作的控制器也会运行,以填充视图所需的任何模型。

Source 资源
Redirect from a view to another view 从视图重定向到另一个视图

Although as @Satpal mentioned, I do recommend you do the redirecting on your controller. 虽然@Satpal提到过,但我建议您在控制器上进行重定向。

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修改为部分。

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 ChildActionOnly attribute 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. ChildActionOnly属性将确保您无法通过导航转到此页面,并且此视图必须呈现为部分视图,因此必须通过父视图呈现。 cfr: Using ChildActionOnly in MVC cfr: 在MVC中使用ChildActionOnly

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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM