STATIC BINDING:
Write Following code in action method in controller
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to the Training Courses...";
List objcourses = new List();
objcourses.Add("Asp.Net");
objcourses.Add("MVC");
objcourses.Add("WCF");
objcourses.Add("WPF");
objcourses.Add("C#.Net");
ViewBag.Courses = new SelectList(objcourses);
return View();
}
}
Then write following code in Related view(Index view)
@{ ViewBag.Title = "Home Page"; }
Index
@using(@Html.BeginForm(“Index”,”Home”,FormMethod.Get)) {
@using(@Html.BeginForm(“Index”,”Home”,FormMethod.Get)) {
Courses List; @Html.DropDownList(“Courses“)
}
DYNAMIC BINDING:
first create database and retrive data from table using sqlclient or entity framework now i am showing using entity framework
Write Following code in action method in controller
public class HomeController : Controller { public ActionResult Index() { private MovieDBContext db = new MovieDBContext(); var GenreLst = new List(); var GenreQry = from d in db.Movies orderby d.Genre select d.Genre; GenreLst.AddRange(GenreQry.Distinct()); ViewBag.Courses = new SelectList(GenreLst); return View(); } } Then write following code in Related view(Index view) @{ ViewBag.Title = "Home Page"; } Index @using(@Html.BeginForm("Index","Home",FormMethod.Get)) { Courses List; @Html.DropDownList("Courses") }
No comments:
Post a Comment