1.first take controller name depController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dropdownbind.EF;
namespace dropdownbind.Controllers
{
public class depController : Controller
{
//
// GET: /dep/
CustomerDBEntities db=new CustomerDBEntities ();
public ActionResult Index()
{
var items = db.Countries.ToList();
SelectList list = new SelectList(items, "CountryId", "Name");
ViewData["CountryList"] = list;
return View();
}
}
}
2. step take class in model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace dropdownbind.Models
{
public class CountryModel
{
public Guid CountryId { get; set; }
public string CountryName { get; set; }
}
}
3.now go to view and do this
@model dropdownbind.Models.CountryModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<div>
@Html.DropDownList("CountryId", (SelectList)ViewData["CountryList"]);
</div>
}
output would be like this-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dropdownbind.EF;
namespace dropdownbind.Controllers
{
public class depController : Controller
{
//
// GET: /dep/
CustomerDBEntities db=new CustomerDBEntities ();
public ActionResult Index()
{
var items = db.Countries.ToList();
SelectList list = new SelectList(items, "CountryId", "Name");
ViewData["CountryList"] = list;
return View();
}
}
}
2. step take class in model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace dropdownbind.Models
{
public class CountryModel
{
public Guid CountryId { get; set; }
public string CountryName { get; set; }
}
}
3.now go to view and do this
@model dropdownbind.Models.CountryModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<div>
@Html.DropDownList("CountryId", (SelectList)ViewData["CountryList"]);
</div>
}
output would be like this-
No comments:
Post a Comment