1

在我的應用程序中,我已將我的UserId與我數據庫中的表關聯。我需要這個,當我創建一個新項目時,我可以從下拉列表中選擇用戶名。並且'可以使用元素viewbag來做到這一點?ASP.NET MVC 3用戶下拉列表

@Html.EditorFor(model => model.UserId) 

我使用默認的成員資格提供這樣我就可以不使用實體框架對於這個問題

編輯

EDIT 2

這是我的行動創造:

[HttpPost] 
public ActionResult Create(Employe employe) 
{ 
     var users = Roles.GetUsersInRole("Admin"); 
     SelectList list = new SelectList(users); 
     ViewBag.Users = list; 
     if (ModelState.IsValid) 
     { 
      **employe.EmployeID = users;** 
      db.Employes.Add(employe); 
      db.SaveChanges(); 
} 

這不起作用。錯誤是:

無法隱式轉換類型 '字符串[]' 到 '字符串'

我對Employee

public class Employee 
{ 
     [Key] 
     public int EmployeID { get; set; } 

     public Guid UserId { get; set; } 

     public string Name { get; set; } 

     [ForeignKey("UserId")] 
     public virtual MembershipUser User 
     { 
      get 
      { 
       return Membership.GetUser(this.Name); //Changed this to Name 
      } 
     } 

    } 
} 

視圖模型:

@Html.DropDownList("Users", ViewBag.Users as SelectList); 

我導致UserId字段不是UserId但這種000000-000000-0000000-00000

回答

1

如何設置用戶的列表作爲ViewBack一個SelectItem 是的,你應該能夠通過你的收集到ViewBag要做到這一點,然後創建你從它下拉列表:

在你的控制器

 var users = Roles.GetUsersInRole("Admin"); 
     SelectList list = new SelectList(users); 
     ViewBag.Users = list; 

在您的視圖(如果你使用剃刀)

@Html.DropDownList("Users", ViewBag.Users as SelectList); 

瞭解更多關於SelectListItem這裏:

還檢查了:

問題更改爲更多。這裏是我的想法來解決這個問題:

控制器

public ActionResult Mirko() { 
    List<SelectListItem> items = new List<SelectListItem>();  
    foreach (string userName in Roles.GetUsersInRole("Admin")) { 
     var user = Membership.GetUser(userName); 
     SelectListItem li = new SelectListItem { 
      Value = user.ProviderUserKey.ToString(), 
      Text = user.UserName, 
     }; 
     items.Add(li); 
    } 
    items.Add(new SelectListItem { Text = "Please Select...", Value = "na" , Selected = true}); 
    ViewBag.Users = items; 
    return View();     
} 

[HttpPost] 
public ActionResult Mirko(Employee employee) { 
    if(IsValideEmployee(employee)) { 
     /*Only used to show that user was retrieved*/ 
     TempData["message"] = "Saved Employee"; 
     TempData["user"] = employee.User; 

     /* employeeRepository.Save(employee) */ 

     /* Redirect to where you want to go */ 
     return RedirectToAction("Mirko", "Home"); 
    } 
    return View(employee); 
} 

private bool IsValideEmployee(Employee emp) { 
    if (emp.Name == "na") 
     ModelState.AddModelError("UserId", "You must select a user!"); 
    /*Do some validation here*/ 
    //ModelState.Add("Name", "You must set the user name!") 
    return ModelState.IsValid; 
} 

查看

@model StackOverFlowExample.Models.Employee 
@{ 
    MembershipUser user = null; 
    ViewBag.Title = "Mirko Example"; 
    var users = ViewBag.Users as IEnumerable<SelectListItem>; 
} 

@if (TempData["message"] != null) { 
    user = TempData["user"] as MembershipUser; 
    <h3>@TempData["message"]</h3> 
    <div> 
     <span>You selected @user.UserName</span> 
     <ul> 
      <li>Email: @user.Email</li> 
      <li>Last Logged In: @user.LastLoginDate.ToString("d")</li> 
      <li>Online: @user.IsOnline</li> 
     </ul> 
    </div> 
} 

@using (@Html.BeginForm()) { 
    <label for="UserId">Associate Employee To User:</label> 
    @Html.DropDownListFor(m => m.UserId, @users) 
    @Html.HiddenFor(m => m.Name)            
    <input type="submit" value="Save" id="save-employee"/> 
} 
<div id="status" style="display:none;"></div> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#UserId").change(function() { 
      //Set value of name 
      $("#Name").val($(this).children("option:selected").text());    
     }); 
     $("#save-employee").click(function (e) { 
      var value = $("#Name").val(); 
      if (value == "" || value == "na") { 
       e.preventDefault(); 
       $("#status").html("<h3>You must select a user!</h3>").toggle(); 
      } 
     }); 
    }); 
</script> 
+0

感謝....取而代之的是 「收集」 我需要一個值GetUserbyRole ()以便我可以看到只有某個角色的用戶,這個方法有一些例子嗎? – Mirko 2012-02-22 17:27:37

+0

你的意思是「Roles.GetUsersInRole(」role name「)」? – anAgent 2012-02-22 18:41:03

+0

是的......我需要試試這個代碼也許在一個viewbag值 – Mirko 2012-02-22 19:28:17