Monday, March 9, 2015

Working with Drop Down list using MVC

File->New->Project



select : ASP.NET MVC 4- Application
AND CLICK ON OK


And select the project template and click on ok




And Go to Controller Folder and Right Click  AddController.




Go to  Controller and Select the Method Index and Right CLick on it.





and Now, go to Index.cshtml


and Write the following Code, ( this is binding Static Data)






Output:







Here, in the above example second parameter is  a collection of SelectListItem  which is used as Datasource for  DropDownList 



2  


public ActionResult Index()

        {
            List<string> ListItems = new List<string>();
            ListItems.Add("Select");
            ListItems.Add("India");
            ListItems.Add("a");
            ListItems.Add("d");
            ListItems.Add("d Africa");
            SelectList Countries = new SelectList(ListItems);
            ViewData["Countries"] = Countries;
            return View();
        }



Index.cshtml




Output:






3

With Model.


Now Go to Solution Explorer




And again go to solution explorer and add new item 
and add ado.net entity frame work




And click on add



Click on New Connection.






Give the Details and Click on Ok Button.






Click on Finish.





Controller.cs




public ActionResult Index()
        {
            RNDDatabaseEntities db = new RNDDatabaseEntities();
            ViewBag.Countries = new SelectList(db.Countries, "CountryID", "Countryname");
            return View();

        }


Index.cshtml


@model IEnumerable<MvcApplication3.Country>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.DropDownList("Countries", "Select")




Output:



4

Controller.cs

Index.cshtml






5





Index.cshtml


@model IEnumerable<MvcApplication3.Models.CountryModel>

@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.DropDownList("SelectedItem", (IEnumerable<SelectListItem>)ViewData["ListItems"])



output:




No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...