ASP.NET MVC 3 Partial View Model object is null

Posted on 2011-10-17
Last Modified: 2012-05-12
I have a created a partial view based off of my ADO.NET entitiy model called ProductList:
@model IEnumerable<MvcPrint.Models.Product>
@foreach (var item in Model) {
            @Html.DisplayFor(modelItem => item.ProductDescription)

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) {
<input type="button" value="Show Products" onclick="getProducts()" />
 <div id="right-box" class="data" style="visibility:visible;">
                   @* Section to display partial forms *@

I click the "Show Products" button, it calls my function just fine:
        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?
Question by:troycomp
    LVL 3

    Accepted Solution

    Model is just in View a Type definition.
    So you have to populate model in ShowProduct, and pass it to View:

            public ActionResult ShowProducts()
                MvcPrint.Models.Product model = .....//Get a list of product here
                return PartialView("ProductList", model );  //Pass this data to View.


    Author Comment

    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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now