【发布时间】:2015-01-09 00:04:57
【问题描述】:
我正在尝试创建一个应用程序,其中有一个 5 度的表,用户可以从下拉列表中选择一个度数。一旦提交,所选学位的 id 应该作为外键保存在 AspNetUser 表中,但这在我的代码中没有发生。相反,每当我注册新用户时,degree_id 列都会留空或“NULL”。我正在使用实体框架来构建我的应用程序。
/* 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,
}
};
我的注册视图模型中有这行 Degrees 代码
[Display(Name = "Degree")]
public Degree Degree { get; set; }
IdentityModels.cs 在 ApplicationUser 下有这行代码:identityUser 类:
public class ApplicationUser : IdentityUser
{
.
.
.
public virtual Degree Degree { get; set; }
.
.
.
.
.
}
我的注册视图如下所示:
<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>
【问题讨论】:
-
您能否确保model.Degree在回发Form时选择了值?
-
否,值未回发
标签: c# asp.net-mvc entity-framework