Model class:
namespace Emp1_MVC.Models
{
using System;
using System.Collections.Generic;
public partial class Emp1MVC
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
}
}
controller:
public class HomeController : Controller
{
ASPEntities dc = new ASPEntities();
private List<SelectListItem> GetCities()
{
List<SelectListItem> cities = new List<SelectListItem>();
cities.Add(new SelectListItem { Value = "1", Text = "Delhi" });
cities.Add(new SelectListItem { Value = "2", Text = "Pune" });
cities.Add(new SelectListItem { Value = "3", Text = "Gurgaon" });
cities.Add(new SelectListItem { Value = "4", Text = "Noida" });
cities.Add(new SelectListItem { Value = "5", Text = "Bangalore" });
cities.Add(new SelectListItem { Value = "6", Text = "Chennai" });
cities.Add(new SelectListItem { Value = "7", Text = "Hyderabad" });
return cities;
}
[HttpGet]
public ActionResult Third()
{
ViewBag.citiesddl = GetCities();
return View();
}
[HttpPost]
public ActionResult Third(Emp1MVC emp)
{
return View(emp);
}
}
View File :
@model Emp1_MVC.Models.Emp1MVC
@{
ViewBag.Title = "Third";
}
<h2>Third</h2>
@using (Html.BeginForm())
{
@Html.Label("lblEmpID", "Employee ID");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@Html.TextBoxFor(c => c.EmpID);
<br />
<br />
@Html.Label("lblEmpName", "Employee Name");
@MvcHtmlString.Create(" ");
@Html.TextBoxFor(c => c.EmpName);
<br />
<br />
@Html.Label("lblCCity", "Employee City");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@Html.DropDownList("ddlCity", new SelectList(ViewBag.citiesddl, "Value", "Text"), "----Select City----");
<br />
<br />
<input type="submit" value="Add" />
}