I have a view Details which consists of a partial view _Tabs and a second partial view _Contents.
In one of the partial views (_Tabs), I have a image link, and in the onclick function, I’m passing a variable CatID.
This variable contains the value of a property of the ViewModel of the partial view (called TabsForItemViewModel).
When debugging, when the image is clicked, VS displays:
Microsoft JScript runtime error: ‘CatID’ is undefined
Partial View “_Tabs”:
View Model for the partial view _Tabs is TabsForItemViewModel
@model TabsForItemViewModel
@{
int CatID = Model.CategoryInfo.CatID;
}
<ul>
<li>
<a href="#GoToCatLevel">
<img border="0" src="view_cat.png" alt ="Up" title="Go To This Category"
width="10" height="10" onclick="LoadCategoryData(CatID, this.href)" />
</a>
</li>
...
View “Details”:
@model ItemDetailsViewModel
@{
ViewBag.Title = "Item Details";
}
<div id="Main">
...
CategoryInfoViewModel categoryData = new CategoryInfoViewModel(itemD.Ca_ID);
TabsForItemViewModel tabsForItem = new TabsForItemViewModel(categoryData);
<div id="Tabs" class="div-tab">
@{Html.RenderPartial("~/Views/Item/_Tabs.cshtml", tabsForItem );}
</div>
...
</div>
CatIDisn’t a Javascript variable… it’s a c# variable that needs to be rendered into the page. Using Razor syntax, this would be: