简体   繁体   中英

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. 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. Instead whenever I'm registering a new user, the degree_id column is being left empty or '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:

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. 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>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM