2012-07-18 20 views
1

嗨,大家好我有一個看起來像這樣如何將列綁定到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])) 

結束 去

+0

夥計們可以幫我在這裏請.......... – SoftwareNerd 2012-07-19 04:29:01

回答

0

讓您獲取動作就像這樣:

[AcceptVerbs(HttpVerbs.Get)] 
public ActionResult AddNew() 
{ 
     ViewBag.RoleId = new SelectList(GetRoles(), "RoleID", "RoleName"); 
     return View(); 
} 

GetRoles應返回Role

列表您認爲:

@Html.DropDownList("RoleId") 

編輯:解釋

Html.DropDownList方法將搜索ViewBagViewData實際上)爲SelectListItem名稱爲RoleId一個列表並使用該下拉列表。您不需要自己明確指定列表。

編輯:糾正GetRoles

GetRoles方法應該是這樣的:

public static List<Role> 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); 

    List<Role> allRoles = new List<Role>(); 
    for(int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { 
     Role role = new Role { 
      RoleId = Convert.ToInt16(ds.Tables[0].Rows[i]["GroupId"]), 
      RoleName = ds.Tables[0].Rows[i]["EmplopyeeRole"].ToString() 
     }; 

     allRoles.Add(role); 
    } 
    conn.Close(); 
    return allRoles; 
} 

我一直與EntityFramework工作。你爲什麼不去那個或其他ORM?

+0

@ Mohayemin..do我必須在我的GetRoles()方法中聲明RoleId,RoleName – SoftwareNerd 2012-07-19 04:32:29

+0

@anilkumar:只需返回在'GetRoles()'方法中來自數據庫的所有'Role'。你的'Role'類具有'RoleId'和'RoleName',不是嗎? – Mohayemin 2012-07-19 04:36:31

+0

@ Mohayemin ..我在我的班上有一個他們,但在下拉列表中我是空的 – SoftwareNerd 2012-07-19 04:42:58

1

想想你將如何去POST選定的角色id回到服務器。

而不是public static List<ResourceModel> GetList { get; set; }使用 public int RoleId {get;set;}。你不能爲List<T>創建一個@Html.DropDownListFor,這是沒有意義的,你只是發佈一個單一的值。

接下來在您的模型中創建一個返回IEnumerableSelectListItem的方法。

public static IEnumerable<SelectListItem> GetRoles() 
    { 
     var roles = //However you plan to get this data from db 
     return roles.Select(o => new SelectListItem 
            { 
             Value = o.RoleID, 
             Text = o.RoleName 
            }); 
    } 

終於在自己的看法:

@Html.DropDownListFor(x=> x.RoleId, ResourceModel.GetRoles())

相關問題