.cshtml
@model RegistrationForm.Models.Employee @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Employee</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.hobbies, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <table> @{ List<RegistrationForm.Models.hobby> mylist = ViewBag.hobbies; foreach(var h in mylist) { <tr> <input type="checkbox" name="hobbies" value="@h.Hobbie" />@h.Hobbie </tr> } } </table> </div> </div> <div class="form-group"> @Html.LabelFor(model => model.gender, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <table> @{ List<RegistrationForm.Models.gender> genders = ViewBag.gender; foreach(var g in genders) { <tr> <input type="radio" name="gender" value="@g.gender1" />@g.gender1 </tr> } } </table> </div> </div> <div class="form-group"> @Html.LabelFor(model => model.state, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.state, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.state, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.city, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.city, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.city, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.picture, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.picture, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.picture, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
Home Controller
using RegistrationForm.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace RegistrationForm.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } // GET: Home/Details/5 public ActionResult Details(int id) { return View(); } // GET: Home/Create public ActionResult Create() { angularEntities db = new Models.angularEntities(); ViewBag.hobbies = db.hobbies.ToList(); ViewBag.gender = db.genders.ToList(); return View(); } // POST: Home/Create [HttpPost] public ActionResult Create(FormCollection collection) { try { // TODO: Add insert logic here angularEntities db = new angularEntities(); string hobbies = collection["hobbies"]; string gender = collection["gender"]; string EmpName = collection["EmpName"]; Employee obj = new Employee(); obj.EmpName = EmpName; obj.gender = gender; obj.hobbies = hobbies; db.Employees.Add(obj); db.SaveChanges(); return Content("OK"); } catch { return View(); } } // GET: Home/Edit/5 public ActionResult Edit(int id) { return View(); } // POST: Home/Edit/5 [HttpPost] public ActionResult Edit(int id, FormCollection collection) { try { // TODO: Add update logic here return RedirectToAction("Index"); } catch { return View(); } } // GET: Home/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: Home/Delete/5 [HttpPost] public ActionResult Delete(int id, FormCollection collection) { try { // TODO: Add delete logic here return RedirectToAction("Index"); } catch { return View(); } } } }
Database Tables Schema Code
CREATE TABLE [dbo].[Employee]( [id] [int] IDENTITY(1,1) NOT NULL, [EmpName] [varchar](50) NULL, [Salary] [int] NULL, [Address] [varchar](50) NULL, [hobbies] [nvarchar](max) NULL, [gender] [varchar](50) NULL, [state] [varchar](50) NULL, [city] [varchar](50) NULL, [picture] [nvarchar](50) NULL, CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO CREATE TABLE [dbo].[hobbies]( [id] [int] IDENTITY(1,1) NOT NULL, [Hobbie] [varchar](50) NULL, [isselected] [bit] NULL, CONSTRAINT [PK_hobbies] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO CREATE TABLE [dbo].[gender]( [id] [int] IDENTITY(1,1) NOT NULL, [gender] [varchar](50) NULL, CONSTRAINT [PK_gender] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
Output
Can you use cascade Dropdownlist for State and City textboxes?