Asp.net mvc mvc中如何使用外键绑定下拉列表

Asp.net mvc mvc中如何使用外键绑定下拉列表,asp.net-mvc,Asp.net Mvc,正在尝试绑定BrandName列下拉列表,但我无法获取它..有人能帮我获取此信息吗 这是我的密码: public class ApplicationUser : IdentityUser { [Column(Order = 1), ForeignKey("Brands")] public int BrandId { get; set; } public virtual Brands Brands { get; set; } } public class Brands

正在尝试绑定BrandName列下拉列表,但我无法获取它..有人能帮我获取此信息吗

这是我的密码:

public class ApplicationUser : IdentityUser
{
    [Column(Order = 1), ForeignKey("Brands")]
    public int BrandId { get; set; }
    public virtual Brands Brands { get; set; }
}

  public class Brands
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string BrandName { get; set; }
    public string ContactPerson { get; set; }
    public string ContactNumber { get; set; }
}
控制器代码:

public ActionResult Register()
{
    ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
        .ToList(), "Name", "Name");
    return View();
}
Register.chstml代码:

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class   = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary()
<div class="form-group">
    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label"    })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.Label("User Role", new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownList("UserRoles", (SelectList)ViewBag.Name, " ", new { @class = "form-control" })
    </div>
</div>
 <div class="form-group">
    @Html.Label("Brand Id", new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownList("BrandId",(SelectList)ViewBag.BrandNamenew,"",new{ @class = "form-control" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Register" />
    </div>
</div>
@使用(Html.BeginForm(“Register”,“Account”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
创建一个新帐户。

@Html.ValidationSummary() @LabelFor(m=>m.UserName,新的{@class=“col-md-2控制标签”}) @TextBoxFor(m=>m.UserName,新的{@class=“form control”}) @LabelFor(m=>m.Password,新的{@class=“col-md-2控制标签”}) @Html.PasswordFor(m=>m.Password,新的{@class=“form control”}) @LabelFor(m=>m.ConfirmPassword,新的{@class=“col-md-2控制标签”}) @Html.PasswordFor(m=>m.ConfirmPassword,new{@class=“form control”}) @Label(“用户角色”,新的{@class=“col-md-2控制标签”}) @DropDownList(“UserRoles”,(SelectList)ViewBag.Name,”,new{@class=“form control”}) @Label(“品牌Id”,新的{@class=“col-md-2控制标签”}) @DropDownList(“BrandId”,(SelectList)ViewBag.BrandNamenew,”,new{@class=“form control”})
}


如何编写控制器和cshtml代码来绑定BrandId下拉列表

什么意思
BrandName
?(模型中唯一的属性是
int BrandId
)使用相关代码编辑您的问题(不在coments中)。只需生成一个角色的
SelectList
newselectlist(db.Brands,“Id”,“BrandName”)
),并将下拉列表绑定到
BrandId
您的意思是什么
BrandName
?(模型中唯一的属性是
int BrandId
)使用相关代码编辑您的问题(不在coments中)。只需生成一个
SelectList
,就像您为角色所做的那样(
newselectlist(db.Brands,“Id”,“BrandName”)
),并将下拉列表绑定到
BrandId