ASP.NET MVC Multiple models binding to a single view.

I came to know that there is a very hot topic that how to bind multiple models to a single view.

I saw developers approached very touch way to acheived this tiny task.Mostly following Implementing IEnumerator Interface. If a developer doesn’t know the complete knowledge about the GetEnumertor function or how to implement and how further bound, It will stuck at some place. I will cut it short.

I suppose you have two classes.

Class 1 named : GetPackages which returns credit card list.

 public class GetPackages
    {
        private List<SelectListItem> _creditCardType =
new List<SelectListItem>();
        public List<SelectListItem> CreditCardTypeList
        {
            get
            {
                _creditCardType.Add(new SelectListItem()
{ Text = "Visa", Value = "Visa" });
                _creditCardType.Add(new SelectListItem()
{ Text = "MasterCard", Value = "MasterCard" });
                _creditCardType.Add(new SelectListItem()
{ Text = "AmericanExpress", Value = "AmericanExpress" });
                _creditCardType.Add(new SelectListItem()
 { Text = "Discover", Value = "Discover" });
                return _creditCardType;
            }
        }
    }

Class2: Month list

public class Month
{
        public List<SelectListItem> MonthList
        {
            get
            {
                _monthList.Add(new SelectListItem()
{ Text = "Jan", Value = "01" });
                _monthList.Add(new SelectListItem()
 { Text = "Feb", Value = "02" });
                _monthList.Add(new SelectListItem()
{ Text = "Mar", Value = "03" });
                _monthList.Add(new SelectListItem()
{ Text = "Apr", Value = "04" });
                _monthList.Add(new SelectListItem()
{ Text = "May", Value = "05" });
                _monthList.Add(new SelectListItem()
{ Text = "Jun", Value = "06" });
                return _monthList;
            }
        }
}

At Controller side:

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            GetPackages getPackage = new GetPackages();
            Month monthlist = new Month();
            ViewBag.GetPackages = getPackage;
            ViewBag.month = monthlist;
            return View();
        }
    }

At View using Razor

 @foreach (var type in ViewBag.GetPackages.CreditCardTypeList)   �
       {     �
           <a href="@type.Text">@type.Text</a>     �
       }  
 @foreach (var month in ViewBag.month.Monthlist)   �
       {     �
           <a href="@month.Text">@month.Text</a>     �
       }

Its a very easy way to bind through Viewbag.
ViewBag which can be used to pass data from Controllers to Views same as you use ViewData[] dictionary.
I am running out of time, I will explain in detail about Viewbag and Viewdata.

Regards,
Joggee.

MVC , , , , ,

2 responses to ASP.NET MVC Multiple models binding to a single view.


  1. hi I was luck to look for your theme in yahoo, your Topics is impressive, I learn a lot in your Topics really thanks very much, btw the theme of you blog is really wonderful, where can find it
    Digital SLR Reviews

  2. Thank you for making the effort to explain the terminlogy for the starters!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>