在我的应用程序中,我已将UserId
与我的数据库中的表相关联。我需要在创建新项目时,我可以从下拉列表中选择用户名。并且'可以使用元素viewbag执行此操作吗?
@Html.EditorFor(model => model.UserId)
我使用默认会员提供商,因此无法使用Entity Framework解决此问题
修改
编辑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();
}
这不起作用。错误是:
无法将类型'string []'隐式转换为'string'
我的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
答案 0 :(得分:1)
如何在ViewBack中将用户列表设置为SelectItem 是的,您应该可以通过将您的集合传递给ViewBag,然后从中创建下拉列表来实现此目的:
在您的控制器中
var users = Roles.GetUsersInRole("Admin");
SelectList list = new SelectList(users);
ViewBag.Users = list;
在您的视图中(如果您使用的是Razor)
@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>