简体   繁体   English

如何在ASP.NET MVC3中删除子操作

[英]How to remove the child action in asp.net mvc3

how can i remove the child action in asp.net mvc3. 我如何在asp.net mvc3中删除子操作。 I mean i am having partial views and main views. 我的意思是我有部分看法和主要看法。 In the url bar when i entered the child page then it should not load. 当我进入子页面时,在网址栏中不会加载它。

for suppose my main page is 假设我的主页是

localhost:5820/Home/Index 本地主机:5820 /首页/索引

Then when i enter 然后当我进入

localhost:5820/Home/Index 本地主机:5820 /首页/索引

then it should work and the child item is 那么它应该工作,子项是

localhost:5820/Home/About 本地主机:5820 /主页/关于

Then when i enter 然后当我进入

localhost:5820/Home/About 本地主机:5820 /主页/关于

then it should not open. 那么它不应该打开。 How can i do that in asp.net mvc. 我如何在asp.net mvc中做到这一点。 I have several pages like that i have displayed the pages in how can i rectify this issue 我有几个这样的页面,我已经显示了如何解决此问题的页面

Looks like you are looking for ChildActionOnly attribute: 看起来您正在寻找ChildActionOnly属性:

[ChildActionOnly]
public ActionResult About()
{
    return View();
}

It disables direct calls to localhost:5820/Home/About - only rendering as partial view will be available. 它禁用对localhost:5820/Home/About直接调用-仅提供部分视图渲染。

UPDATE according to what you need - mark all actions which should return partial views with ChildActionOnly attribute: 根据您的需要进行更新 -使用ChildActionOnly属性标记所有应返回部分视图的操作:

public ActionResult Index()
{
    return View();
}

[ChildActionOnly]
public ActionResult About()
{
    var model = ...
    return PartialView("_About", model);
}

And in index view call those actions (from Home controller and other controllers) via: 然后在索引视图中通过以下命令调用这些操作(从Home控制器和其他控制器):

@Html.Action("About", "Home")

The fact that your ABOUT page is a link inside your HOME page does not mean it is a child action, in fact, from the point of view of MVC they are in the same level, is YOU as a programmer that give the ilussion of hiereachies by providing an order of navigation. 您的ABOUT页面是HOME页面中的链接这一事实并不意味着它是子操作,实际上,从MVC的角度来看,它们处于同一个级别,是您作为程序员的错觉通过提供导航顺序。

Now, what you can do is: 现在,您可以做的是:

  1. simply remove the Action Methods from your controller, or 只需从控制器中删除操作方法,或

  2. write a simple ignore route like: 写一个简单的忽略路由,例如:

    routes.IgnoreRoute("YourRuleToIgnoreActions"); route.IgnoreRoute(“ YourRuleToIgnoreActions”);

this rules should be at the beginning of your RegisterRoutes method in RegisterRoutes to avoid other rules to be triggered 此规则应位于RegisterRoutes中RegisterRoutes方法的开头,以避免触发其他规则

To build up your ignore rules, here is a nice discussion on the topic: http://ardalis.com/IgnoreRoute-in-ASP.NET-Routing-is-Order-Dependent 要建立您的忽略规则,这里是有关该主题的精彩讨论: http : //ardalis.com/IgnoreRoute-in-ASP.NET-Routing-is-Order-Dependent

UPDATE : Probably too much thinking on this one, but in my opinion, the Models CAN have properties that modify the behaviour of the View, this is because from an architectural point of view, when you are already in the View, your concern is exclusively on the presentation layer, so I think it is perfectly valid to have something like: 更新 :可能对此问题考虑过多,但我认为模型可以具有修改视图行为的属性,这是因为从体系结构的角度来看,当您已经在视图中时,您的关注点就完全是在表示层上,因此我认为具有以下内容是完全有效的:

 public class MyReportsModel{
     public bool displaySection1 { get; set;}
    //other data
 }

and in your views, you can change the presentation in the following way: 在您看来,您可以通过以下方式更改演示文稿:

  @{
   if(@Model.displaySection1){
        //display as normal
    }

}

Of course, when you populate the model you should set that property in each controller according to your needs: 当然,当您填充模型时,应根据需要在每个控制器中设置该属性:

  MyReporstModel thisView = new MyReportsModel();
  thisView.displaySection1 = true;
   // set all properties necessary to display

   // if the controller knows that this partial view won't be displayed then 
    thisView.displaySection1 = false;

But that is no longer a technical issue but an architectural one. 但这不再是技术问题,而是架构问题。

hope it helps, 希望能帮助到你,

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

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