简体   繁体   中英

How to remove the child action in asp.net mvc3

how can i remove the child action in 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

Then when i enter

localhost:5820/Home/Index

then it should work and the child item is

localhost:5820/Home/About

Then when i enter

localhost:5820/Home/About

then it should not open. How can i do that in 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]
public ActionResult About()
{
    return View();
}

It disables direct calls to localhost:5820/Home/About - only rendering as partial view will be available.

UPDATE according to what you need - mark all actions which should return partial views with ChildActionOnly attribute:

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:

@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.

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");

this rules should be at the beginning of your RegisterRoutes method in RegisterRoutes to avoid other rules to be triggered

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

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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