简体   繁体   English

使用EF将表的主键映射为AspNetUser表作为外键

[英]Mapping of primary key of a table into AspNetUser Table as foreign key using EF

I'm trying to create an application where I have a table of 5 degrees and a user can select a degree from the drop down list. 我正在尝试创建一个具有5度表的应用程序,用户可以从下拉列表中选择一个度。 Once submitted, the selected degree's id is suppose to be saved in the AspNetUser table as foreign key but that is not happening in my code. 提交后,所选学位的ID应该作为外键保存在AspNetUser表中,但这在我的代码中没有发生。 Instead whenever I'm registering a new user, the degree_id column is being left empty or 'NULL'. 取而代之的是,每当我注册新用户时,degree_id列都将保留为空或为'NULL'。 I'm using entity framework to build my application. 我正在使用实体框架来构建我的应用程序。

/* This is my Account/Register Controller */ 
[HttpPost] 
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser
        {
            UserName = model.Email,
            Email = model.Email,
            FirstName = model.FirstName,
            LastName = model.LastName,
            StudentId = model.StudentId,
            Degree = model.Degree,
            UserProfileInfo = new UserProfileInfo
            {
                CurrentCourses = model.CurrentCourse,
                TakenCourses = model.TakenCourse,
                PlannedCourses = model.PlannedCourse,
                appointment = model.Appointment,
            }
        };

I have this line of code for Degrees in my register view model 我的寄存器视图模型中具有学位的这一行代码

[Display(Name = "Degree")]
public Degree Degree { get; set; }

IdentityModels.cs has this line of code under ApplicationUser : identityUser class: IdentityModels.cs在ApplicationUser下有以下代码行:identityUser类:

public class ApplicationUser : IdentityUser
{
.
.
.  
public virtual Degree Degree { get; set; } 
.
.
.
.
.
} 

And my register view looks like this: 我的注册视图如下所示:

<div class="form-group">
 @Html.LabelFor(model => model.Degree, "Degree", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
 @Html.DropDownList("Degrees", new SelectList(ViewBag.Degrees, "Id", "Title"), "-- Select Degree --", htmlAttributes: new { @class = "form-control" })
</div>

No the value is not posting back 没有值不回传

Based on your comment, DropDownList is not configured properly. 根据您的评论, DropDownList配置不正确。 It is why you cannot retrieve the posted value. 这就是为什么您无法检索过帐的值。

Look at this example - 看这个例子-

Controller 调节器

public class YourController : Controller
{
    public ActionResult Index()
    {
        var model = new RegisterViewModel();

        model.Degrees = new List<SelectListItem>
        {
            new SelectListItem { Text = "One", Value = "1"},
            new SelectListItem { Text = "Two", Value = "2"},
            new SelectListItem { Text = "Three", Value = "3"},
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(RegisterViewModel model)
    {
        string degreeId = model.SelectedDegreeId;

    }
}

Model 模型

public class RegisterViewModel
{
    public string SelectedDegreeId { get; set; }

    public IEnumerable<SelectListItem> Degrees { get; set; }
}

View 视图

@using (Html.BeginForm())
{
    @Html.DropDownListFor(x => x.SelectedDegreeId, Model.Degrees)
    <button type="submit">Submit</button>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将外键添加到 AspNetUser 表 - Add foreign key to AspNetUser table 使用ASPNETUSER表电子邮件字段作为另一个表中的主键,即主外键 - Use ASPNETUSER Table Email Field as a primary key in another table, primary foreign key 同一主键ApplicationUser表上的EF多个外键关系 - EF multiple foreign key relationship on same primary key ApplicationUser table EF-如何定义外键,即当前表的主键 - EF - How to define foreign key which is the primary key of the current table 相同表EF6中对主键的外键引用 - Foreign Key References to Primary Key in the Same Table EF6 如何将AspNetUser表ID作为外键映射到另一个表 - How to map AspNetUser table id as foreign key to another table 使用代码优先创建表时添加引用AspNetUser的外键-MVC5 - Adding a Foreign Key referencing AspNetUser when creating a table using Code First - MVC5 实体框架-尝试使用外键插入表中,EF尝试使用主键插入原始表中 - Entity Framework - trying to insert into a table with a foreign key, EF attempts to insert into the original table with the primary key nhibernate通过外键而不是主键连接到表 - nhibernate join to table by foreign key not primary key EF 4.3 Code First:具有复合主键和外键的每类型表(TPT) - EF 4.3 Code First: Table per Type (TPT) with Composite Primary Key and Foreign Key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM