Create ASP.NET MVC Framework 4.7.2 based application.
Step1. Create Models/Book
namespace MVCDropdown1.Models
{
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public string Author { get; set; }
}
}
using MVCDropdown1.Models;
using System.Collections.Generic;
using System.Web.Mvc;
namespace MVCDropdown1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
List list = new List();
Book b1 = new Book() { Id = 1, Name = "Java Fundamentals", Price = 200, Author = "Mark" };
Book b2 = new Book() { Id = 2, Name = "Economics for Beginners", Price = 200, Author = "Bhim Mehta" };
Book b3 = new Book() { Id = 3, Name = "Physics Fundamentals", Price = 456, Author = "N.C. Roy" };
Book b4 = new Book() { Id = 4, Name = "C++ Fundamentals", Price = 789, Author = "Robins" };
//Add books to List
list.Add(b1);
list.Add(b2);
list.Add(b3);
list.Add(b4);
//Create SelectList object
ViewBag.authors = new SelectList(list, "Id", "Author");
return View();
}
}
}
Step3. Create Views/Home/View
<h2>Index</h2>
@Html.DropDownList("dd", ViewBag.authors as SelectList, new { onchange = "" })
Output
No comments:
Post a Comment