How to bind data to dropdown control of View (.cshtml) using MVC4.0?


Are you an MVC 4.0 beginner? Learn how to bind data to a dropdown control view using MVC 4.0 in this article. This is a little tricky but probably this sample code will make it a little easier for you to understand.

I would like to share tips and tricks in my MVC4.0 related articles so that it will guide MVC 4.0 beginners.

Binding different controls in views become quite tricky sometimes. A Web-API is a defined set of HTTP request messages with proper definition of the structure of response messages.

If you are using Razor then development part is challenging if you do not know how to go ahead. You have to play around with HTML5,jQuery in .cshtml pages.
If you want to retrieve data from database at runtime and bind it in dropdown control then you can use following code:


classname clsObj = null;
[HttpGet]
public ActionResult Create()
{

var httpclientobj = new HttpClient();
clsObj = new classname();
var my_contents = httpclientobj.GetAsync("http://localhost:your_port_number/api/your_api_controller_name")
.ContinueWith((resultant_task) =>
{
var response = resultant_task.Result;
var contents_read = response.Content.ReadAsAsync<List<lstinClass>>();
contents_read.Wait();
clsObj.lstinClass = new List();
clsObj.lstinClass.AddRange(contents_read.Result);
});
my_contents.Wait();
IEnumerable data_retrieval_list = from ob in clsObj.lstinClass
select new SelectListItem
{
Text = ob.column_name,
Value = ob.column_name.ToString()
};
ViewBag.temp_view_bag = data_retrieval_list;
return View();
}

How it works?
I have used create as an ActionResult in which I have mentioned api controller's uri so that I can keep all business login regarding different functionalities in WEB-API controller.
I have created object of HttpClient and task is created.
[HttpGet] is used because this method is called for reading contents. We are aware that in MVC4.0, we have different verbs such as GET, POST, PUT, DELETE.
contents_read.Result is filled by WEB-API code. You can also host your application in IIS and test.
IEnumerable data_retrieval_list contains all data retrieved from the database and we are binding it with dropdownbox in .cshtml page.
ViewBag.temp_view_bag has to mentioned in .cshtml page.
Code in view is as follows:

@Html.DropDownListFor(m=>m.propertyname , (IEnumerable)ViewBag.temp_view_bag)

Here we have bound the dropdown with data collection so that when you run the application, you will find data in dropdownbox.
There are different ways to persist data between multiple methods of same page or requests between multiple views. ViewBag is one of it.
Sessions can also be maintained.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: