c# - How can I reuse a controller for multiple partial views? -


Dynamically select a partial view based on what I send to the controller, and manage it But I think I am A) It is more than complicated and B) How to get all the page links easily. Will be addressed in every turn if I make a mistake below, please note, it is from memory! All this works at home, I am after clarification whether there is any better way!

First of all, what I have done:

I have a controller:

  Private dictionary < Int, string & gt; PagesForFolder = New Dictionary & lt; Int, string & gt; () {... here I have my list of pages (index and pagename pairs)} Public ActionResult Test (int id = -1) {try {string pageName = ""; Var result = pagesForFolder.TryGetValue (id, name of page); If (result) string directory otherwise = "~ view / folder a / _" + page name + ".cshtml"; Return partial view (directory); } Hold (Exception Pre) {...} Return View (); }  

Then in the folder I see all my partial thoughts for that section of my website. I have several sections and 1 controller per controller. However, I am thinking that This will not be great because I have to rebuild every time I add a page. Would it be better to store pages in DB?

I think your current approach is quite reasonable. I think that as your problem, you have mentioned that whenever you add or remove new entries in the dictionary, you have to re-create your application.

As a solution for this, you can store your page names in DB and when necessary.

Try something like this rather than having a hard coded dictionary ...

In your controller ..

  Public Functional Testing (int Id = -1) {try {string pagename = GetYourPageFromDB (id); If (! String.IsNullOrEmpty (PageName)) string directory = "~ view / folderA / _" + PageName + ".cshtml"; Return partial view (directory); } Hold (Exception Pre) {...} Return View (); }  

This can not be an exact answer, try something like that ...


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -