我有一个叫 Sammanhang 的模型
[Key]
public int SammanhangsID { get; set; }
public string Namn { get; set; }
我想将用户的 ID 作为外键包括在内,这样我就可以获得数据库中所有用户的下拉列表。
我尝试做这样的事情
public class Sammanhang
{
[Key]
public int SammanhangsID { get; set; }
public string Namn { get; set; }
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
和 IdentityModels 内部
public class ApplicationUser : IdentityUser
{
public virtual Sammanhang Sammanhang { get; set; }
}
但是没有成功,有没有办法实现用户下拉列表?
最佳答案
如果需要快速设置某些内容,请尝试使用 SelectListItem 和匿名类型。
@Html.DropDownList("Users", new List<SelectListItem>
{
new SelectListItem { Text = "John" },
new SelectListItem { Text = "Frank" },
new SelectListItem { Text = "Joe" }
}, "Select User")
或者使用现有模型,
@using (Html.BeginForm()) {
<fieldset>
<legend>Select Users</legend>
<div class="editor-field">
@Html.ListBox("Users", ViewBag.Userslist as MultiSelectList
)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
关于c# - Mvc 标识 : Adding UserId to another table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36713810/