Asp.Net MVC Archives - Page 2 of 2 - Coding Sharp

Asp.Net MVC

Bind DropDownList in Asp.MVC using ViewBag object

Bind DropDownList in Asp.MVC using ViewBag object

Asp.Net, Asp.Net MVC, C#.Net
In our previous article we have seen how to bind fixed value from view and how to bind Range to dropdown list, In this article we will see how to bind ViewBag data to dropdown. Remember we can set any data to ViewBag like from database, from enum, from range, fixed value etc. Here we will take some fixed country list and save it to the ViewBag and then in view we will bind ViewBag value to the dropdown as shown in below code snippet. Controller code Razor View code Above code will produce output as below image. In the controller if you see we have created a list of SelectListItem object, and then added 1 by 1 value to the list. After adding all the required items to list, we have assigned it to the ViewBag object, In razor view we have used that ViewBag data for ...
Bind DropDownList in Asp.MVC using Range/Fixed value

Bind DropDownList in Asp.MVC using Range/Fixed value

Asp.Net, Asp.Net MVC, C#.Net
In most of the application we are using dropdown to select some value from predefined list of value. Some of them are getting bind from the database table, some of are getting bind using Enum value while some of them are getting bind using some fix value like 1 to 10, or month drop-down 1 to 12. In this article post we will see how to bind drop-down using range value in MVC. Below if the sample code which will bind 2000 to 2014 in year drop-down. above code will produce output as below image. In the above code we have just write a code in view only, there is no server side code. Now in some cases we need to bind some data which are not in particular range like "2000, 2002, 2004, 2006, 2008" or some string type fixed data then we can not use "Enumerable.Range", so how can w...