Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After messing around with this for over a week, I finally managed to figure out how MVC can do what I want dynamically. I decided to post my solution for others that are new to MVC. Hopefully, the following will clear up the misunderstandings I had (although, at this point in my understanding of MVC, I cannot say this is the best approach.)</p> <p>I will include the previous code snips and modifications for clarity:</p> <pre><code> public interface IModuleRepository { IList&lt;MenuItemModule&gt; GetMenuItemModules(int menuItem); IList&lt;Announcements&gt; GetAnnouncements(int modID); IList&lt;News&gt; GetNews(int modID); IList&lt;Contacts&gt; GetContacts(int modID); } //business object public class MenuItemModule { private int _MenuItemID; private int _ModuleID; private int _ModuleDefID; private string _Src; private int _ModuleOrder; //get, set properties for these... } //announcements entity public class Announcements : MenuItemModule { private int _ID = -1; private string _Description = string.Empty; //get set props ... } </code></pre> <p>I also added another class:</p> <pre><code>public class AnnouncementModule : MenuItemModule { private IList&lt;Announcements&gt; _Announcements; //get set prop } </code></pre> <p>...and I created a model for the view</p> <pre><code>public class HomeItemViewModel { public MenuItemModule[] MenuItemModules { get; set; } //collection of menuitemmodules } </code></pre> <p>In my home controller... </p> <pre><code> var menuItemModules = modRepository.GetMenuItemModules(ItemID); if (menuItemModules.Count &gt; 0) { AnnouncementModule aMod; MenuItemModule[] mods = new MenuItemModule[menuItemModules.Count()]; int i = 0; //loop through each MenuItemModule assign to the appropriate model foreach (MenuItemModule mod in menuItemModules) { if (mod.Src == "Announcements") { aMod = new AnnouncementModule(); aMod.Announcements = modRepository.GetAnnouncements(mod.ModuleID); //now add this to the menuitemmodule collection mods[i] = aMod; } if (mod.Src == "Contacts") { //... } i++; } } var viewModel = new HomeItemViewModel { MenuItemModules = mods }; return View(viewModel); </code></pre> <p>Then I used the suggestion to use DisplayFor in the view. The view is strongly typed to HomeItemViewModel.</p> <pre><code>&lt;%: Html.DisplayFor(m =&gt; m.MenuItemModules) %&gt; </code></pre> <p>This iterates through the collection and based on the type, it will call that template. In this example, it calls AnnouncementModule.ascx which is strongly typed to AnnouncementModule.</p> <pre><code>foreach (var a in Model.Announcements) { //a.Description will give us the description of the announcement } </code></pre> <hr> <p>I realize there are slicker ways to code the controller, and I plan on refactoring, but this skeleton should provide the basics to solve the question I posted. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload