Link to home
Start Free TrialLog in
Avatar of troycomp
troycomp

asked on

ASP.NET MVC 3 Partial View Model object is null

I have a created a partial view based off of my ADO.NET entitiy model called ProductList:
@model IEnumerable<MvcPrint.Models.Product>
<table>
    <tr>
        <th>
            ProductDescription
        </th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProductDescription)
        </td>
    </tr>
}
</table>

The namespace MvcPrint.Models.Product points to a product table in my database.
I want to load it when a button is clicked, Heres my Index.cshtml code on the homecontroller:
@{
    ViewBag.Title = "Home Page";
}
<script type="text/javascript">
    function getProducts() {
        $.get('/Products/ShowProducts/', function (data) {
            $('div#right-box.data').html(data);
        });
    }
            </script>
<h2>@ViewBag.Message</h2>
<input type="button" value="Show Products" onclick="getProducts()" />
 <div id="right-box" class="data" style="visibility:visible;">
                   @* Section to display partial forms *@
                </div>

I click the "Show Products" button, it calls my function just fine:
[HttpGet]
        public ActionResult ShowProducts()
        {
            return PartialView("ProductList");
        }

But the Model is null on the ProductList partial view.
@foreach (var item in Model)

 What am I forgetting to do so the model will return a list of products?
ASKER CERTIFIED SOLUTION
Avatar of Marcin_Zawadzki
Marcin_Zawadzki

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of troycomp
troycomp

ASKER

That was it. I thought the view has a life cycle and the code to generate the list of products would be executed when the view was called.

Thank you