嗨,大家好我有一個看起來像這樣如何將列綁定到DropDownList並獲取MVC3中的選定值?
usertable RoleTable
----------------------- ---------------------------
UserID|UserName|Pwd|RoleID RoleID|RoleName
1 |Anil |123|1 1 |Admin
現在我在表中顯示,在那裏他有一個AddNew鏈接的用戶表,當的AddNew聯繫管理員點擊我會告訴他的AddNew頁面,兩個表他有一些標籤和文本框AddNew用戶,
現在我想要做的是在AddNew頁面我想在DropDown中顯示所有RoleNames,以便管理員可以選擇用戶必須在哪個角色.. ..and我想要檢索選定的數據
這是我的模型類
public class ResourceModel
{
public static List<ResourceModel> GetList { get; set; }
public Int16 EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeEmailId { get; set;}
public string GroupName { get; set; }
public string EmployeePassword { get; set; }
}
這是我的控制器
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AddNew()
{
ViewBag.Roles = new SelectList(GetRoles(),"RoleID","RoleName");
return View();
}
//
//Geting All Roles In a GetRoles()/
//
public static GetRoles()
{
SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
SqlCommand Cmd = new SqlCommand("Select GroupId,EmplopyeeRole from EmployeeGroup", conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(Cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
//roles.Add(new SelectListItem{.Value=i,.Text=i};
var model = new ResourceModel();
model.RoleId = Convert.ToInt16(ds.Tables[0].Rows[i]["GroupId"]);
model.RoleName = ds.Tables[0].Rows[i]["EmplopyeeRole"].ToString();
//roles.Value=Convert.ToString(model.RoleId);
//roles.Text=model.RoleName;
}
conn.Close();
return ds ;
}
我有什麼在上面的代碼
這是我的AddNew aspx頁面
<div>
<% using (Html.BeginForm())
{ %>
<%-- <form action="Create.aspx" method="post"></form>--%>
<%:Html.ValidationSummary(true)%>
<fieldset>
<legend style="color:Orange; font-weight:bolder;">AddNew Project</legend>
<div class="editor-label" style="color:Orange; font-weight:bolder;">
<%: Html.LabelFor(model => model.EmployeeName)%>
</div>
<div class="editor-field">
<%:Html.EditorFor(model => model.EmployeeName)%>
<%: Html.ValidationMessageFor(model => model.EmployeeName)%>
</div>
<div class="editor-label" style="color:Orange; font-weight:bolder;">
<%:Html.LabelFor(model => model.EmployeeEmailId)%>
</div>
<div class="editor-field">
<%:Html.EditorFor(model => model.EmployeeEmailId)%>
<%:Html.ValidationMessageFor(model => model.EmployeeEmailId)%>
</div>
<div class="editor-label" style="color:Orange; font-weight:bolder;">
<%:Html.LabelFor(model => model.EmployeePassword)%>
</div>
<div class="editor-field">
<%:Html.EditorFor(model => model.EmployeePassword)%>
<%:Html.ValidationMessageFor(model => model.EmployeePassword)%>
</div>
<div class="editor-label" style="color:Orange; font-weight:bolder;">
<%:Html.LabelFor(model => model.GroupName)%>
</div>
<div class="editor-field">
<%:Html.EditorFor(model => model.GroupName)%>
<%:Html.ValidationMessageFor(model => model.GroupName)%>
<p>
<input type="submit" value="Create" style="color:Orange; font-weight:bolder;"/>
</p>
</fieldset>
<%} %>
</div>
任何一個可以幫助我恢復這樣做,請在MVC3 ....我必須顯示RoleNames在DropDown我必須retreive選擇在我的[AcceptVerbs(HttpVerbs.Post)]中的值控制器....任何想法,請
我有一個存儲程序爲本人的[AcceptVerbs(HttpVerbs.Post)]其中與所選擇的下拉值i將插入ROLENAME的角色ID以dB
Create Procedure InsertEmplyoee
(
@EmployeeName varchar(50),
@EmployeeEmailId varchar(50),
@EmployeePassword varchar (50),
@GroupName varchar (50)
)
as
begin
insert into EmployeeDetails (EmployeeName,EmployeeEmailId,EmployeePassword,GroupId) values
(@EmployeeName,@EmployeeEmailId,@EmployeePassword,(select GroupId from EmployeeGroup where [email protected]))
結束 去
夥計們可以幫我在這裏請.......... – SoftwareNerd 2012-07-19 04:29:01