加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Mvc标识:将UserId添加到另一个表

发布时间:2020-12-15 20:59:20 所属栏目:百科 来源:网络整理
导读:我有一个名为Sammanhang的模型 [Key]public int SammanhangsID { get; set; }public string Namn { get; set; } 我想将用户的id作为外键包含在内,以便我可以获得数据库中所有用户的下拉列表. 我考虑做这样的事情 public class Sammanhang{ [Key] public int
我有一个名为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>

更多信息:http://www.asp.net/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读