Multiple Model In Single View MVC

Last Reply on May 05, 2016 04:18 AM By Shashikant

Posted on May 05, 2016 02:09 AM

I want to Add product and News ( Two models) with in a single view

Please help me with simple login

News.cs In Model Folder

public class News
    {
        public int NewsID { get; set; }
        public string NewsItem { get; set; }
    }

Product.cs In Model Folder

 

 public class Product
    {
        public int       ProdID                   { get; set; }
        public string    ProdName                 { get; set; }
        public string    ProdManufacturer         { get; set; }
        public Decimal   ProdPrice                { get; set; }
        public DateTime? ProdManufacturingDate    { get; set; }
        public DateTime?  ProdExpiryDate           { get; set; }
    }

HomeController

 

 #region "product data Object"
        IEnumerable<Product> product = new List<Product>
        {
            new Product(){ProdID=1,ProdName="Pavilion dv2z",ProdManufacturer="H P",ProdPrice=1123.09M   },

            new Product(){ProdID=2,ProdName="Inspiron",ProdManufacturer="Dell",ProdPrice=1223.29M       },
           
            new Product(){ProdID=3,ProdName="MacBook",ProdManufacturer="Apple",ProdPrice=3123.43M       },
            
            new Product(){ProdID=4,ProdName="MacBook Air",ProdManufacturer="Apple",ProdPrice=4123.10M   },
            
            new Product(){ProdID=5,ProdName="MacBook Pro",ProdManufacturer="Apple",ProdPrice=4123.89M  },
           
            new Product(){ProdID=6,ProdName="Mac Mini",ProdManufacturer="Apple",ProdPrice=2235.87M   },
           
            new Product(){ProdID=7,ProdName="Sonic F15",ProdManufacturer="Vivo",ProdPrice=1236.09M },
            
            new Product(){ProdID=8,ProdName="Yoga Series",ProdManufacturer="Lenovo",ProdPrice=2123.39M },
            
            new Product(){ProdID=9,ProdName="Galaxy Note Series",ProdManufacturer="Samsung",ProdPrice=9123.60M  },
            
            new Product(){ProdID=10,ProdName="Galaxy Note 10.1",ProdManufacturer="Samsung",ProdPrice=5123.78M  },
            
            new Product(){ProdID=11,ProdName="VivoBook S200E",ProdManufacturer="Asus",ProdPrice=1293.29M  },
           
            new Product(){ProdID=12,ProdName="Aspire E",ProdManufacturer="Acer",ProdPrice=1238.11M}
        };
        #endregion

        #region "News Data Object"
        IEnumerable<News> news = new List<News>()
        {
           new News{NewsID=1,NewsItem="IPL 2016, KKR vs KXIP: We knew 160 was defendable on this ground, says Andre Russell"},
           new News{NewsID=2,NewsItem="Apple hires Google X lab co-founder for health projects"},
           new News{NewsID=3,NewsItem="Planet Nine' may not exist, scientists say"},
           new News{NewsID=4,NewsItem="Soon, a botanical drug to cure dengue!"},
        };
        #endregion

        #region "Index Controller"
        public ActionResult Index()
        {
            IEnumerable<Product> prd = product.ToList();
            return View(prd);
        }
        #endregion

Please Help me to show both model class data with in a single View

 

 


Posted on May 05, 2016 04:18 AM

Hi,

Please refer below code.

Index(View)

<div>
    <h2>
        Product Details
    </h2>
    <table>
        <tr>
            <th>
                ProdID
            </th>
            <th>
                ProdName
            </th>
            <th>
                ProdManufacturer
            </th>
            <th>
                ProdPrice
            </th>
        </tr>
        <% foreach (var item in Model.Products)
            { %>
        <tr>
            <td>
                <%: Html.DisplayFor(x=>item.ProdID) %>
            </td>
            <td>
                <%: Html.DisplayFor(x=>item.ProdName) %>
            </td>
            <td>
                <%: Html.DisplayFor(x=>item.ProdManufacturer) %>
            </td>
            <td>
                <%: Html.DisplayFor(x=>item.ProdPrice) %>
            </td>
        </tr>
        <% } %>
    </table>
    <h2>
        News Section</h2>
    <marquee direction="left" scrollamount="2" loop="true" width="100%" bgcolor="#ffffff">
    <table>
        <% foreach (var item in Model.News)
            { %>
        <tr>
            <td>
                <%: Html.ActionLink(item.NewsItem,null) %>
            </td>
        </tr>
        <% } %>
    </table>
</marquee>
</div>

HomeController(Controller)

public ActionResult Index()
{
    IEnumerable<Product> products = new List<Product>
        {
            new Product(){ProdID=1,ProdName="Pavilion dv2z",ProdManufacturer="H P",ProdPrice=1123.09M   },
 
            new Product(){ProdID=2,ProdName="Inspiron",ProdManufacturer="Dell",ProdPrice=1223.29M       },
           
            new Product(){ProdID=3,ProdName="MacBook",ProdManufacturer="Apple",ProdPrice=3123.43M       },
            
            new Product(){ProdID=4,ProdName="MacBook Air",ProdManufacturer="Apple",ProdPrice=4123.10M   },
            
            new Product(){ProdID=5,ProdName="MacBook Pro",ProdManufacturer="Apple",ProdPrice=4123.89M  },
           
            new Product(){ProdID=6,ProdName="Mac Mini",ProdManufacturer="Apple",ProdPrice=2235.87M   },
           
            new Product(){ProdID=7,ProdName="Sonic F15",ProdManufacturer="Vivo",ProdPrice=1236.09M },
            
            new Product(){ProdID=8,ProdName="Yoga Series",ProdManufacturer="Lenovo",ProdPrice=2123.39M },
            
            new Product(){ProdID=9,ProdName="Galaxy Note Series",ProdManufacturer="Samsung",ProdPrice=9123.60M  },
            
            new Product(){ProdID=10,ProdName="Galaxy Note 10.1",ProdManufacturer="Samsung",ProdPrice=5123.78M  },
            
            new Product(){ProdID=11,ProdName="VivoBook S200E",ProdManufacturer="Asus",ProdPrice=1293.29M  },
           
            new Product(){ProdID=12,ProdName="Aspire E",ProdManufacturer="Acer",ProdPrice=1238.11M}
        };
    IEnumerable<News> news = new List<News>()
        {
            new News{NewsID=1,NewsItem="IPL 2016, KKR vs KXIP: We knew 160 was defendable on this ground, says Andre Russell"},
            new News{NewsID=2,NewsItem="Apple hires Google X lab co-founder for health projects"},
            new News{NewsID=3,NewsItem="Planet Nine' may not exist, scientists say"},
            new News{NewsID=4,NewsItem="Soon, a botanical drug to cure dengue!"},
        };

    ProductDetailsModel productDetailsModel = new ProductDetailsModel
    {
        Products = products,
        News = news
    };
    return View(productDetailsModel);
}

Product(Model)

public class Product
{
    public int ProdID { get; set; }
    public string ProdName { get; set; }
    public string ProdManufacturer { get; set; }
    public Decimal ProdPrice { get; set; }
    public DateTime? ProdManufacturingDate { get; set; }
    public DateTime? ProdExpiryDate { get; set; }
}

News(Model)

public class News
{
    public int NewsID { get; set; }
    public string NewsItem { get; set; }
}

ProductDetailsModel(Model)

public class ProductDetailsModel
{
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<News> News { get; set; }
}

Screenshot