我有一个ASP.NET MVC 5应用程序,其中用户(雇主)注册从默认的帐户/注册操作中输入电子邮件和密码,然后重定向(如果成功)到另一个操作(雇主/创建)。
我在项目中有一个雇主模型(Employer.cs
),其中包含电子邮件作为其中一个属性。当显示视图(雇主/创建)时,我希望禁用电子邮件字段,但仍然显示来自AspNetUsers
表(用于注册的表)的电子邮件,以便用户无法输入与AspNetUsers
表中的一个。
这是我的代码:
帐户/注册
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-group", role = "form" }))
{
<div class="col-md-4" id="log">
<h3>Register</h3>
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.TextBoxFor(m => m.Email, new { placeholder = "Email", @class = "form-control, textbox" })
}
和雇主/创建
@using (Html.BeginForm("Create", "Employer", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="col-md-6" id="log">
<h3>Welcome</h3>
<h4>Kindly create a profile to connect with talented techies</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.TextBoxFor(model => model.Email, htmlAttributes: new { @class = "form-control,text-box", placeholder = "Email address", value=TempData["Email"], @readonly ="readonly" })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
}
和雇主控制器
public ActionResult Create([Bind(Include = "ID,CompanyName,Email,CompanyWebsite,Address,Field")] Employer employer)
{
employer.Email = User.Identity.GetUserName();
TempData["Email"] = employer.Email;
try
{
if (ModelState.IsValid)
{
db.Employers.Add(employer);
db.SaveChanges();
return RedirectToAction("Details", "Employer", new { Message = ManageController.ManageMessageId.CreateProfileSuccess, id = Model.ID });
}
}
...
}
我尝试使用TempData
,但无济于事。我该如何上班?
注册操作:
public async Task<ActionResult> Register(RegisterViewModel model)
{
using (var context = new ApplicationDbContext())
if (ModelState.IsValid)
{
var user = new ApplicationUser { 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);
userManager.AddToRole(user.Id, "Employer");
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
...
return RedirectToAction("Create", "Employer");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
创建操作的HttpGet请求
public ActionResult Create()
{
return View();
}