创建使用asp.标识以预填充一些数据并创建数据库的新条目



我有一个正在使用ASP.Identity创建新用户的网站。我有一个包含更多用户信息的辅助表,我希望在注册帐户后填充这些信息。由于我已经有了id和电子邮件,我不需要再要求这些,但无法获得第二个表单来通过Create Razor页面传递数据,即使它显示在表单上。

当我提交时,我收到一条错误消息,说"LoginID是必需的",那么我如何获得带有userLoginId值的表单?

创建页面

var userLoginId = User.Identity.GetUserId();
<div class="form-horizontal">
<h4>nrLogins</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.LoginID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => userLoginId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => userLoginId, "", new { @class = "text-danger" })
</div>
</div>

登录控制器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "LoginID,Email,FirstName,LastName,DateOfBirth,Address1,Address2,Address3,Address4,Address5,PostCode,PhoneMobile,PhoneOther,UserSearchable,SiteRoleType,AccountActive,AccountCreatedDate,AccountEditedDate,AccountDeletedDate")] dbLogin dbLogin)
{
if (ModelState.IsValid)
{
db.dbLogins.Add(dbLogin);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(dbLogin);
}

谢谢!

您需要为控件指定name属性,否则它默认为您的变量名userLoginId而不是LoginId,因此您可以执行以下操作:

@Html.EditorFor(model => userLoginId, null, "LoginId", new { htmlAttributes = new { @class = "form-control" } })

如果你不想让用户看到或编辑详细信息,你可以将其作为隐藏字段传递

@Html.Hidden("LoginId", userLoginId)

最新更新