Posts

Showing posts with the label Different ways to pass multiple models

Passing multiple models in single view .

There are two different ways of doing this . * Passing parent view that contains both the models which we want to pass in the view . * passing each model in a ViewBag we have two different models public class Customer { public int CustomerID {get; set;} public string CustomerName {get; set;} } public class Product { public int ProductId {get; set ;} public string ProductName{get; set ;} } 1. By passing a parent view Here we want to pass both the models in a single view , by default the razor engine only accepts single model per view. so we need to create a parent view that hold both the models. public class ParentView { public Customer customer{get; set;} public Product product{get ; set;} } then we can call the model in the view by using the following code in view page : @model iEmumerable <models.ParentView> ForEach(var v in ParentView) { < option > @v.customer.CustomerID < / option > < option > @v.customer.CustomerName <...