Hi to all.I've started learning ASP.NET MVC for the past two weeks.Can anyone help me in storing DropDownList selected value into database For data purpose I've created a Entity Data Model from an existing database and i've created class file from that model with the help of code generation items with EF5 DbContect Generator package.Here I'm sending my Model,Controller and View codes Please help me regarding this scenario.
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{//// GET: /Home/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" />}