注册用户时将下拉列表中的值插入到用户配置文件表中
本文关键字:用户 插入 配置文件 下拉列表 注册 | 更新日期: 2023-09-27 18:30:37
我正在开发一个 Asp.Net 的MVC应用程序,其中我使用默认的" Register
"页面将新用户插入到具有Id,UserName and Password
字段的默认UserProfile
表中。但是,根据我的要求,我又添加了一个名为CompanyId
的字段,该字段引用了不同表中的Company name
。要将CompanyId
插入用户配置文件表中,我在注册页面中使用了一个下拉列表,其中包含company names
列表,但我不确定如何在注册用户时插入 Id。
我正在使用以下 HTML 作为注册中的公司列表.cshtml
<li>
@Html.LabelFor(m => m.CompanyId)
<select data-bind="options: $root.counterpartyNames, optionsText: 'CounterPartyName', optionsValue: 'Id', value: selectedBidCounterParty, optionsCaption: 'Company'">
</select>
</li>
我不确定如何引用Drop Down list
中的值以从默认Register ActionResult
插入到UserProfile
[HttpPost]
//[AllowAnonymous]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
// WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
有没有更好的方法可以做到这一点?
还有
第三个可选参数要WebSecurity.CreateUserAndAccount
,允许您传递其他用户数据。只需将您的代码更改为:
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { CompanyId = model.CompanyId });
当然,您还需要修复 HTML select
元素,以便在您获得任何内容之前,它实际上有一个名称来保存已发布的值:
<select name="CompanyId" ...>
最好是使用 Razor 帮助程序,以便自动生成正确的名称属性:
@Html.DropDownListFor(m => m.CompanyId, new List<SelectListItem>(), new { data_bind = "... knockout stuff ..." })