Poul K. Sørensen
Poul K. Sørensen

Reputation: 17530

How to create a view with subviews in asp.net MVC 4

I have a controller:

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

MySite/MyController/ renders the site with 3 buttons and a div. Buttons redirect to: MySite/Controller/button1 MySite/Controller/button2 MySite/Controller/button3

I would like that when the users clicks button1 it renders MySite/MyController/ with the content of public ActionResult Button1() in the div.

same goes for botton 2 and 3. And if the users go directly to MySite/Controller/button2 it should render the Index with ActionResult Button2 in the div from the beggining.

Is there an easy way to do this? Do i need to do ajax to replace the content of the div, or should i just make the buttons a partial view and render them on the 4 pages.

If its easy with ajax, i would like that as the page dont need to refresh, but just load in the content. But i am not sure how i would make it render the pages correctly if the user goes to MySite/Controller/button2 directly.

Upvotes: 1

Views: 1116

Answers (1)

sbking
sbking

Reputation: 7680

There are a few valid options that I see here:

  1. Have each button load a new page with their content on it.
  2. Include each button content as a PartialView in a single page, and use javascript to change the div content without AJAX.
  3. Don't include any of the button contents, and load them all through AJAX.
  4. Include the first visible button content, and load the others through AJAX (if these are static content then you can design your script to store the values so each one only has to load once.

Personally, I would try to do a combination of 1 and either 2, 3, or 4 (depending on use case). This way the basic functionality of the site works without javascript, but you can still get the benefits of jQuery/AJAX.

Upvotes: 0

Related Questions