C# 使用默认的User.Identity.getUserName()从AspNetUsers表提取电子邮件到另一个视图(从另一个表),并禁用该字段
我有一个ASP.NET MVC 5应用程序,其中用户(雇主)从默认帐户/注册操作注册输入电子邮件和密码,然后重定向(如果成功)到另一个操作(雇主/创建) 我在项目中有一个雇主模型(C# 使用默认的User.Identity.getUserName()从AspNetUsers表提取电子邮件到另一个视图(从另一个表),并禁用该字段,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我有一个ASP.NET MVC 5应用程序,其中用户(雇主)从默认帐户/注册操作注册输入电子邮件和密码,然后重定向(如果成功)到另一个操作(雇主/创建) 我在项目中有一个雇主模型(employer.cs),其中一个属性是电子邮件。显示视图(雇主/创建)时,我希望禁用电子邮件字段,但仍显示来自AspNetUsers表(用于注册的)的电子邮件,以便用户无法输入与AspNetUsers表中不同的邮件 这是我的密码: 帐户/登记 @using (Html.BeginForm("Register", "A
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" })
}
我尝试使用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);
}
您遇到了哪些问题?您的
创建
操作在保存后会重定向到详细信息
操作,并且已将电子邮件地址设置为只读。我想我们需要您的注册
操作、详细信息
视图以及您正在使用的模型。@BrendanGreen注册操作现在已经完成included@kat1330电子邮件字段被禁用(由于readonly属性),但来自注册操作的电子邮件不会传递给它,即它是空的;因为您没有将模型传递给Create
操作。您在创建HttpGet
(而不是HttpPost
)操作中使用的是什么模型?您遇到了哪些问题以及在哪里遇到的问题?保存后,您的Create
操作将重定向到Details
操作,并且它已将电子邮件地址设置为只读。我想我们需要您的注册
操作、详细信息
视图以及您正在使用的模型。@BrendanGreen注册操作现在已经完成included@kat1330电子邮件字段被禁用(由于readonly属性),但来自注册操作的电子邮件不会传递给它,即它是空的;因为您没有将模型传递给Create
操作。您在创建HttpGet
(而不是HttpPost
)操作中使用的是什么模型?
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 });
}
}
...
}
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);
}
public ActionResult Create()
{
return View();
}