将 MVC5 应用程序放在一起,我想做的是让用户注册为客户或商人。登录详细信息可以进入创建的相同默认 [AspNetUsers] 表,但如果他们是商人,我希望收集其他详细信息。当我查看我正在构建的应用程序附带的一些代码时,我对下一步应该做什么或者即使我使用的方法是否正确感到有些困惑。我的目标是收集任意一组注册信息,具体取决于填写的信息。
我的帐户/注册 Controller 开始看起来像这样:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
using (var context = new ApplicationDbContext())
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
FirstName = model.FirstName,
LastName = model.LastName,
UserName = model.Email,
Email = model.Email
};
var result = await UserManager.CreateAsync(user, model.Password);
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
if (result.Succeeded)
{
if (model.CompanyName.Trim().Length > 0)
{
var company = new CompanyModel
{
CompanyName = model.CompanyName,
AddressLine1 = model.AddressLine1,
AddressLine2 = model.AddressLine2,
Town = model.Town,
County = model.County,
Postcode = model.Postcode,
TelNo = model.TelNo,
MobNo = model.MobNo,
GoogleVerified = model.GoogleVerified,
FacebookProfileUrl = model.FacebookProfileUrl
};
//insert company details?
}
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
所以我的问题是我正在做的正确/好事,我可以通过同一个 Controller 收集两种类型用户的详细信息吗?
此外,在 Register.cshtml 页面中,我有一个 jquery Accordion ,其想法是客户可以填写一个部分并加入,或者商人可以单击 Accordion ,它会显示另一个要填写的部分。然后我面临的问题是我使用过这行代码一次:
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
然后将 Accordion 控件放在其中,这意味着我有这些控件
@Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
两次,一次为用户,一次为商人,是否允许?
最佳答案
添加 <label>
两次就可以了,但我假设您必须拥有关联的 @Html.TextBoxFor(m => m.FirstName)
也重复,这不好,因为 (1) 它会生成重复的 id
无效的 html 属性和 (2) DefaultModelBinder
仅当第一个输入为 name="FirstName"
时才会绑定(bind)该值.
没有理由重复控制。相反,装饰与您的 CompanyModel
关联的 View 模型属性具有条件验证属性(例如 foolproof [RequiredIf]
),因此如果用户也是商人,则需要它们。既然您已经表明用户可以是商人也可以不是,我建议 bool
属性来生成复选框。您的 View 模型将如下所示
public class RegisterViewModel
{
// Properties associated with ApplicationUser
[Display(Name = "Company Name")]
[Required(ErrorMessage = "Please enter your First Name")]
public string FirstName { get; set; }
....
public bool IsTradesPerson { get; set; }
// Properties associated with CompanyModel
[Display(Name = "Company Name")]
[RequiredIfTrue("IsTradesPerson", ErrorMessage = "Please enter your Company Name")]
public string CompanyName { get; set; }
....
}
并在 View 中放置与 CompanyModel
关联的控件模型隐藏<div>
元素,以便可以根据 IsTradesPerson
切换其可见性属性(property)
<div id="usercontrols">
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
....
@Html.LabelFor(m => m.IsTradesperson)
@Html.CheckBoxFor(m.IsTradesperson)
</div>
<div id="tradespersoncontrols" hidden>
@Html.LabelFor(m => m.CompanyName)
@Html.TextBoxFor(m => m.CompanyName)
@Html.ValidationMessageFor(m => m.CompanyName)
....
<div>
以及切换可见性的脚本
var controls = $('tradespersoncontrols');
$('#IsTradesperson').click(function) {
if ($(this).is(':checked')) {
controls.show();
} else {
controls.hide();
}
});
然后在 POST 方法中您可以检查 if(model.IsTradesperson)
如果是这样,请创建您的 CompanyModel
基于 View 模型属性并保存它。
关于c# - 在 MVC5 应用程序中注册期间获取附加信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212904/