如何在MVC剃须刀中绑定下拉列表 [英] How to Bind Dropdown list in MVC razor
问题描述
大家好,
我是MVC 4.0中的新手,我正在通过新技术练习自己。我被困在州的绑定下拉列表中,然后在州的基础上获得区域。
我发布我的模型,视图和控制器代码。
提供的解决方案非常受欢迎。
型号:
公共类EmployeeRegitrationModel
{
[必填(ErrorMessage =请输入名称)]
[显示(姓名=员工姓名)]
公共字符串EMP_NAME {get;组; }
[必填(ErrorMessage =请输入完整地址)]
[显示(姓名=员工地址)]
public string EMP_ADDRESS {get;组; }
[必填(ErrorMessage =请输入手机号码)]
[显示(姓名=员工手机)]
[DataType(DataType.PhoneNumber)]
public string EMP_MOBILE {get;组; }
[必填]
[显示(姓名=员工电子邮件)]
[数据类型( DataType.EmailAddress)]
公共字符串EMP_EMAIL {get;组; }
public string STATE_CODE {get;组; }
公共字符串STATE_NAME {get;组; } $ / $
public int EmployeeRegitration(EmployeeRegitrationModel用户)
{
SqlConnection con = new SqlConnection(timeout = 200; Server = lsipl_dc; Database = TestMVC; User Id = sa; Password = Lsipl#2013);
SqlCommand cmd = new SqlCommand(PROC_EMPREGISTRATION,con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ QTYPE,1);
cmd.Parameters.Add(@ EMP_NAME,user.EMP_NAME);
cmd.Parameters.Add(@ EMP_ADDRESS,user.EMP_ADDRESS);
cmd.Parameters.Add(@ EMP_MOBILE,user.EMP_MOBILE);
cmd.Parameters.Add(@ EMP_EMAIL,user.EMP_EMAIL);
int i = cmd.ExecuteNonQuery();
con.Close();
返回i;
}
public List< employee regitrationmodel> GetAllState()
{
List< employeeregitrationmodel> GetState = new List< employeeregitrationmodel>();
SqlConnection con = new SqlConnection(timeout = 200; Server = lsipl_dc; Database = TestMVC; User Id = sa; Password = Lsipl#2013);
con.Open();
SqlCommand cmd = new SqlCommand(GETALLSTATE,con);
SqlDataReader dr;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ QTYPE,1);
dr = cmd.ExecuteReader();
if(dr.Read())
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
user.STATE_CODE = dr [CODE]。ToString();
user.STATE_NAME = dr [NAME]。ToString();
GetState.Add(user);
}
返回GetState;
}
公共列表< employeeregitrationmodel> GetEmployeeDetails()
{
List< employeeregitrationmodel> GetRecodrs = new List< employeeregitrationmodel>();
SqlConnection con = new SqlConnection(timeout = 200; Server = lsipl_dc; Database = TestMVC; User Id = sa;密码= Lsipl#2013);
con.Open();
SqlCommand cmd = new SqlCommand(PROC_EMPSHOWALL,con);
SqlDataReader dr;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ QTYPE,1);
dr = cmd.ExecuteReader();
while(dr.Read())
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
user.EMP_NAME = dr [EMP_NAME]。ToString();
user.EMP_ADDRESS = dr [EMP_ADDRESS]。ToString();
user.EMP_MOBILE = dr [EMP_MOBILE]。ToString();
user.EMP_EMAIL = dr [EMP_EMAI L]。ToString();
GetRecodrs.Add(用户);
}
con.Close();
返回GetRecodrs;
}
}
======控制器:
公共类EmployeeRegitrationController:Controller
{
//
// GET:/ EmployeeRegitration /
[HttpGet]
公共ActionResult索引()
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
List< employeeregitrationmodel> strlist = new List< employeeregitrationmodel>();
strlist = user.GetAllState();
return View(strlist);
} < br $>
[HttpPost]
公共ActionResult索引(EmployeeRegitrationModel用户)
{
user.EmployeeRegitration(user);
返回查看();
}
[HttpGet]
public ActionResult GetAllEmpDetails()
{
EmployeeRegitrationModel emp = new EmployeeRegitrationModel();
List< employeeregitrationmodel> strlist = new List< employeeregitrationmodel>();
strlist = emp.GetEmployeeDetails();
return View(strlist);
}
}
==========================查看
@model MyFirst_SimpleMVCProject.Models.EmployeeRegitrationModel
@ {
ViewBag.Title =员工注册;
}
< hgroup class =title >
@ ViewBag.Title。
@ *
创建一个新帐户。
* @@using(Html.BeginForm())
{
@ Html.AntiForgeryToken ()
@ Html.ValidationSummary()
员工登记表
- @ Html.LabelFor(m => m.EMP_NAME)
@ Html.TextBoxFor(m => m.EMP_NAME)
- @ Html.LabelFor(m => m.EMP_ADDRESS)
@ Html.TextBoxFor(m => m.EMP_ADDRESS)
- @ Html.LabelFor(m => m .EMP_MOBILE)
@ Html.TextBoxFor(m => m.EMP_MOBILE)
- @ Html.LabelFor(m => m。 EMP_EMAIL)
@ Html.TextBoxFor(m => m.EMP_EMAIL)
- @ *这里我想得到下拉列表并绑定与州长* @
@ * @ Html.LabelFor(m => m.STATE_NAME)* @
@ * @ Html.DropDownList(title)* @
< input type =submitvalue =注册/>
}
查看coloumn名称是否为Statename或
控制器 - > ActionResult Index()
//获取要在下拉列表中加载的国家/地区列表
ViewBag.State = new SelectList(user.GetAllState(),ID,StateName,ID);
观看次数 - > Index.cshtml
@ Html.DropDownList(State,ViewBag.State as SelectList,
Select State,new {style =width:200px})
Hi All,
I am very new in MVC 4.0, I am doing practice own self over new technology. I am stuck at binding dropdown list for state and after that get district on bases of state.
I am posting my model, view and controller code.
provide solution is highly appreciated.
MODEL:
public class EmployeeRegitrationModel
{
[Required(ErrorMessage = "Please Enter the Name")]
[Display(Name = "Employee Name")]
public string EMP_NAME { get; set; }
[Required(ErrorMessage = "Please Enter the full Address ")]
[Display(Name = "Employee Address")]
public string EMP_ADDRESS { get; set; }
[Required(ErrorMessage = "Please Enter Mobile Number")]
[Display(Name = "Employee Mobile")]
[DataType(DataType.PhoneNumber)]
public string EMP_MOBILE { get; set; }
[Required]
[Display(Name = "Employee Email")]
[DataType(DataType.EmailAddress)]
public string EMP_EMAIL { get; set; }
public string STATE_CODE { get; set; }
public string STATE_NAME { get; set; }
public int EmployeeRegitration(EmployeeRegitrationModel user)
{
SqlConnection con = new SqlConnection("timeout=200; Server=lsipl_dc; Database=TestMVC;User Id=sa;Password=Lsipl#2013");
SqlCommand cmd = new SqlCommand("PROC_EMPREGISTRATION", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@QTYPE", 1);
cmd.Parameters.Add("@EMP_NAME", user.EMP_NAME);
cmd.Parameters.Add("@EMP_ADDRESS", user.EMP_ADDRESS);
cmd.Parameters.Add("@EMP_MOBILE", user.EMP_MOBILE);
cmd.Parameters.Add("@EMP_EMAIL", user.EMP_EMAIL);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
public List<employeeregitrationmodel> GetAllState()
{
List<employeeregitrationmodel> GetState = new List<employeeregitrationmodel>();
SqlConnection con = new SqlConnection("timeout=200; Server=lsipl_dc; Database=TestMVC;User Id=sa;Password=Lsipl#2013");
con.Open();
SqlCommand cmd = new SqlCommand("GETALLSTATE", con);
SqlDataReader dr;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@QTYPE", 1);
dr = cmd.ExecuteReader();
if (dr.Read())
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
user.STATE_CODE = dr["CODE"].ToString();
user.STATE_NAME = dr["NAME"].ToString();
GetState.Add(user);
}
return GetState;
}
public List<employeeregitrationmodel> GetEmployeeDetails()
{
List<employeeregitrationmodel> GetRecodrs = new List<employeeregitrationmodel>();
SqlConnection con = new SqlConnection("timeout=200; Server=lsipl_dc; Database=TestMVC;User Id=sa;Password=Lsipl#2013");
con.Open();
SqlCommand cmd = new SqlCommand("PROC_EMPSHOWALL", con);
SqlDataReader dr;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@QTYPE", 1);
dr = cmd.ExecuteReader();
while (dr.Read())
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
user.EMP_NAME = dr["EMP_NAME"].ToString();
user.EMP_ADDRESS = dr["EMP_ADDRESS"].ToString();
user.EMP_MOBILE = dr["EMP_MOBILE"].ToString();
user.EMP_EMAIL = dr["EMP_EMAIL"].ToString();
GetRecodrs.Add(user);
}
con.Close();
return GetRecodrs;
}
}
======Controller:
public class EmployeeRegitrationController : Controller
{
//
// GET: /EmployeeRegitration/
[HttpGet]
public ActionResult Index()
{
EmployeeRegitrationModel user = new EmployeeRegitrationModel();
List<employeeregitrationmodel> strlist = new List<employeeregitrationmodel>();
strlist = user.GetAllState();
return View(strlist);
}
[HttpPost]
public ActionResult Index(EmployeeRegitrationModel user)
{
user.EmployeeRegitration(user);
return View();
}
[HttpGet]
public ActionResult GetAllEmpDetails()
{
EmployeeRegitrationModel emp = new EmployeeRegitrationModel();
List<employeeregitrationmodel> strlist = new List<employeeregitrationmodel>();
strlist = emp.GetEmployeeDetails();
return View(strlist);
}
}
==========================View
@model MyFirst_SimpleMVCProject.Models.EmployeeRegitrationModel
@{
ViewBag.Title = "Employee Registration";
}
<hgroup class="title">
@ViewBag.Title.
@*
Create a new account.
*@@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
Employee Registration Form
- @Html.LabelFor(m => m.EMP_NAME)
@Html.TextBoxFor(m => m.EMP_NAME)
- @Html.LabelFor(m => m.EMP_ADDRESS)
@Html.TextBoxFor(m => m.EMP_ADDRESS)
- @Html.LabelFor(m => m.EMP_MOBILE)
@Html.TextBoxFor(m => m.EMP_MOBILE)
- @Html.LabelFor(m => m.EMP_EMAIL)
@Html.TextBoxFor(m => m.EMP_EMAIL)
- @* Here I want to get the dropdown list and bind with state master *@
@*@Html.LabelFor(m => m.STATE_NAME)*@
@* @Html.DropDownList("title")*@
<input type="submit" value="Register" />
}
See if coloumn name is Statename or not Controllers -> ActionResult Index() //Get list of Countries to load on dropdownlist ViewBag.State= new SelectList(user.GetAllState(), "ID", "StateName", "ID"); Views -> Index.cshtml @Html.DropDownList("State", ViewBag.State as SelectList, "Select State", new { style = "width:200px" })
这篇关于如何在MVC剃须刀中绑定下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!