I have about 15 partial views that I need to display based upon user's menu tab selection. Basically I have these 15 menu tabs on the side and based on user click for these tabs, I will be displaying the content for that tab on the page.
Also I am using Knockout here.
So I have these 15 Knockout observables (self.tab_A(), self.tab_B(), ...self.tab_N()
) populated when the page first loads.
The code I have is something like this. Here is the view.
<ul id="tabs">
<li>
<a data-bind="click: function(){ $root.ShowHideDiv(tab_A.id); }" style="cursor: pointer;">
Tab A
</a>
</li>
<li>
<a data-bind="click: function(){ $root.ShowHideDiv(tab_B.id); }" style="cursor: pointer;">
Tab B
</a>
</li>
...
...
</ul>
The partial views are pre-loaded but hidden from from user's view.
<ul>
<li id="tab_A" style="display: none" class="hiddenDivs">
@{Html.RenderPartial("~/Views/Claims/FroiReport/_Insurer.cshtml");}
</li>
<li id="tab_B" style="display: none" class="hiddenDivs">
@{Html.RenderPartial("~/Views/Claims/FroiReport/_Insurer.cshtml");}
</li>
</ul>
Displaying div using script on click event.
self.ShowHideDiv = function (tabToShow) {
console.log(tabToShow);
$('.hiddenDivs').html('');
$('#' + tabToShow).show();
};
Now, the problem is that the UI using this code is working fine for 3-4 views but the design is breaking for the remaining views possibly because there are too many divs which are being hidden (I am not sure here).
So, I was looking into other options where I will load the specific view at run-time only. When user clicks on a tab, get the partial view and load it.
Hence, I tried something like this:
self.ShowHideDiv = function (tabToShow) {
$('.hiddenDivs').html('');
var view = getValueFromDict(dict, tabToShow); //gets the needed view from a dictionary in "~/Views/Products/CoolProducts/_ItemOne.cshtml" format
$('.hiddenDivs').load('/Claims/ReturnPartialView/?partialViewName=' + view);
};
But this doesn't work since I do not have any Action/Controller associated with these views, I am unable to load the view directly using javascript/jquery.
Another option I have tried is creating a controller that returns a partial view.
public ActionResult ReturnPartialView(string partialViewName)
{
return PartialView(partialViewName);
}
and calling it like this:
self.ShowHideDiv = function (tabToShow) {
$('.hiddenDivs').html('');
var view = getValueFromDict(dict, tabToShow);
$('.hiddenDivs').load('/MyController/ReturnPartialView/?partialViewName=' + view);
}
Now this loads the view that I need but the KnockOut observable associated with the view is coming as null doing this.
I heard that KO templates can be used for my scenario but have not yet tried KO templates to solve this (which I am going to look into next). I would like to see if anyone has solution to my problem without using the KO templates.
Thanks much for your patience to trying to understand this.