I am making an user registration form in that i want to populate my drop down list in drop down the value for gender come from enum and the contray and city come from data base.
making an application in asp.net mvc4.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Medical.Models
{
public class Registration
{
public int User_Id { get; set; }
public int Role_Id { get; set; }
[Required(ErrorMessage = "Please Enter Email Address")]
[Display(Name = "UserName")]
[RegularExpression(".+@.+\\..+", ErrorMessage = "Please Enter Correct Email Address")]
public string Email { get; set; }
[Required(ErrorMessage = "Please Enter Password")]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required(ErrorMessage = "Please Enter Confirm Password")]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string CPassword { get; set; }
public string First_Name { get; set; }
public string Middle_Name { get; set; }
[Required(ErrorMessage = "Please Enter Last Name")]
[Display(Name = "Last Name")]
public string Last_Name { get; set; }
[Required(ErrorMessage = "Please Enter Date Of Birth")]
[Display(Name = "Birth Date")]
public DateTime Dob { get; set; }
[Display(Name="Gender")]
public SelectList gender { get; set; }
[Display(Name = "LandLine1")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Landline1 { get; set; }
[Display(Name = "LandLine2")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Landline2 { get; set; }
[Required(ErrorMessage = "Please Enter Mobile No")]
[Display(Name = "Mobile No")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Mobile1{ get; set; }
[Display(Name = "Mobile No")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Mobile2{ get; set; }
[Display(Name = "Designitation")]
public string Designitation { get; set; }
}
}
controller:
--------------
using System.Web.Mvc;
using Medical.DataAcessLayer;
using Medical.Models;
using System.Collections.Generic;
namespace Medical.Controllers
{
public class RegistrationController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Register() // Calling when we first hit controller.
{
return View();
}
[HttpPost]
public ActionResult Register(Registration MD) // Calling on http post (on Submit)
{
if (ModelState.IsValid) //checking model is valid or not
{
DataAcessLayer.RegistrationDB objDB2 = new DataAcessLayer.RegistrationDB(); //calling class DBdata
string result = objDB2.InsertData1(MD);// passing Value to DBClass from model
ViewData["result"] = result;
ModelState.Clear(); //clearing model
return View();
}
else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
}
public System.Collections.IEnumerable theComponentTypes { get; set; }
}
}
View:
-----------------
@model Medical.Models.Registration
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
Register</h2>
@using (Html.BeginForm())
{
<table width="50%">
<tr>
<td>
@Html.Label("Role")
@Html.TextBoxFor(a => a.Role_Id)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Email)
@Html.TextBoxFor(a => a.Email)
@Html.ValidationMessageFor(a => a.Email)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Password)
@Html.TextBoxFor(a => a.Password)
@Html.ValidationMessageFor(a => a.Password)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.CPassword)
@Html.TextBoxFor(a => a.CPassword)
@Html.ValidationMessageFor(a => a.CPassword)
</td>
</tr>
<tr>
<td>
@Html.Label("First Name")
@Html.TextBoxFor(a => a.First_Name)
@Html.ValidationMessage("Enter First Name")
</td>
<td>
</td>
</tr>
<tr>
<td>
@Html.Label("Middle Name")
@Html.TextBoxFor(a => a.Middle_Name)
@Html.ValidationMessage("Enter First Name")
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Last_Name)
@Html.TextBoxFor(a => a.Last_Name)
@Html.ValidationMessageFor(a => a.Last_Name)
</td>
<td>
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Dob)
@Html.TextBoxFor(a => a.Dob)
@Html.ValidationMessageFor(a => a.Dob)
</td>
</tr>
<tr>
<td>
@Html.Label("Gender")
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Landline1)
@Html.TextBoxFor(a => a.Landline1)
@Html.ValidationMessageFor(a => a.Landline1)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Landline2)
@Html.TextBoxFor(a => a.Landline2)
@Html.ValidationMessageFor(a => a.Landline2)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Mobile1)
@Html.TextBoxFor(a => a.Mobile1)
@Html.ValidationMessageFor(a => a.Mobile1)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Mobile2)
@Html.TextBoxFor(a => a.Mobile2)
@Html.ValidationMessageFor(a => a.Mobile2)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Designitation)
@Html.TextBoxFor(a => a.Designitation)
@Html.ValidationMessageFor(a => a.Designitation)
</td>
</tr>
<tr>
<td colspan="2">
<input id="Submit1" type="submit" value="submit" />
</td>
</tr>
</table>
}