C# 编辑纵断面图asp.net-mvc5
我是asp.net mvc5的新手,在我的应用程序中,我想在他们的个人资料中检索用户信息。 在配置文件操作中,我试图显示用户信息,并允许他们编辑自己的信息。 但是当我运行程序时,我得到的编辑和显示视图是空的。 例如,当用户尝试编辑时,我希望看到他们以前的信息。 以下是我想要的图片: 这是我的行动:C# 编辑纵断面图asp.net-mvc5,c#,asp.net-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Identity,我是asp.net mvc5的新手,在我的应用程序中,我想在他们的个人资料中检索用户信息。 在配置文件操作中,我试图显示用户信息,并允许他们编辑自己的信息。 但是当我运行程序时,我得到的编辑和显示视图是空的。 例如,当用户尝试编辑时,我希望看到他们以前的信息。 以下是我想要的图片: 这是我的行动: [HttpGet] public ActionResult DriverProfile() { var userManag
[HttpGet]
public ActionResult DriverProfile()
{
var userManager = HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
var authManager = HttpContext.GetOwinContext().Authentication;
ProfileModel driver = new ProfileModel();
IdentityUser theUser = new IdentityUser() { UserName = driver.email, Email = driver.email };
driver = new ProfileModel(theUser);
return View("DriverProfile", driver);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DriverProfile(ProfileModel profile)
{
var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(new RidesDbContext()));
IdentityUser theUser = new IdentityUser() { UserName = profile.email, Email = profile.email };
IdentityResult theResult = manager.Create(theUser, profile.PasswordHash);
var currentUser = manager.FindById(User.Identity.GetUserId());
currentUser.UserName = profile.email;
currentUser.Email = currentUser.Email;
return Redirect("DriverProfile");
}
如何使用编辑选项在他们的个人资料中获取用户信息
谢谢你的帮助。多谢各位
编辑:
视图如下所示:
public class ProfileModel
{
public ProfileModel()
{
}
public ProfileModel(IdentityUser theUser)
{
}
[Display(Name = "Id")]
public int driverId { get; set; }
[Display(Name = "First Name")]
[Required(ErrorMessage = "Pleas Enter Your First Name")]
public string firstName { get; set; }
[Display(Name = "Last Name")]
[Required(ErrorMessage = "Pleas Enter Your Last Name")]
public string lastName { get; set; }
[Display(Name = "Email Address")]
[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Pleas Enter Your Email Address")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please Enater a Valid Email Address")]
public string email { get; set; }
[Display(Name = "Mobile Number")]
[Required(ErrorMessage = "Pleas Enter Your Mobile Number")]
public string phoneNumber { get; set; }
[Display(Name = "Address")]
[Required(ErrorMessage = "Pleas Enter Your Address")]
public string Address { get; set; }
[Display(Name = "City")]
[Required(ErrorMessage = "Pleas Enter Your City")]
public string city { get; set; }
[Display(Name = "State")]
[Required(ErrorMessage = "Pleas Enter Your state")]
public string state { get; set; }
[Display(Name = "Car")]
[Required(ErrorMessage = "Please Identify Your Car")]
public string car { get; set; }
[Display(Name = "Driver's License")]
[Required(ErrorMessage = "Please Enter Your Driver's Licende Number")]
public string driverslicense { get; set; }
[Display(Name = "Profile Image")]
[Required]
public byte[] profileImg { get; set; }
public string profileImgType { get; set; }
[Display(Name = "License Image")]
[Required]
public byte[] licenseImg { get; set; }
public string licenseImgType { get; set; }
[Display(Name = "Password")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Please Enter a password")]
public string PasswordHash { get; set; }
}
@model RidesApp.Models.ProfileModel
@{
ViewBag.Title = "DriverProfile";
Layout = "~/Views/Shared/DriversViewPage.cshtml";
}
<h2>DriverProfile</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ProfileModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.driverId)
<div class="form-group">
@Html.LabelFor(model => model.firstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.firstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.firstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.lastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.lastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.lastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.phoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.phoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.phoneNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.city, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.city, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.city, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.state, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.state, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.state, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.car, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.car, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.car, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.driverslicense, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.driverslicense, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.driverslicense, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.profileImgType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.profileImgType, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.profileImgType, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.licenseImgType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.licenseImgType, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.licenseImgType, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.PasswordFor(model => model.PasswordHash, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PasswordHash, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PasswordHash, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Edit Profile" class="btn btn-default" />
</div>
</div>
</div>
}
@model.app.Models.ProfileModel
@{
ViewBag.Title=“DriverProfile”;
Layout=“~/Views/Shared/DriversViewPage.cshtml”;
}
驱动器配置文件
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
轮廓模型
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.driverId)
@LabelFor(model=>model.firstName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.firstName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.firstName,“,new{@class=“text danger”})
@LabelFor(model=>model.lastName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.lastName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.lastName,“,new{@class=“text danger”})
@LabelFor(model=>model.email,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.email,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.email,“,new{@class=“text danger”})
@LabelFor(model=>model.phoneNumber,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.phoneNumber,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.phoneNumber,“,new{@class=“text danger”})
@LabelFor(model=>model.Address,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Address,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Address,“,new{@class=“text danger”})
@LabelFor(model=>model.city,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.city,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.city,“,new{@class=“text danger”})
@LabelFor(model=>model.state,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.state,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.state,“,new{@class=“text danger”})
@LabelFor(model=>model.car,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.car,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.car,“,new{@class=“text danger”})
@LabelFor(model=>model.driverslicense,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.driverslicense,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.driverslicense,“,new{@class=“text danger”})
@LabelFor(model=>model.profileImgType,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.profileImgType,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.profileImgType,“,new{@class=“text danger”})
@LabelFor(model=>model.licenseImgType,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.licenseImgType,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.licenseImgType,“,new{@class=“text danger”})
@Html.PasswordFor(model=>model.PasswordHash,新的{@class=“control label col-md-2”})
@EditorFor(model=>model.PasswordHash,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PasswordHash,“,new{@class=“text danger”})
}
如果我正确理解了问题,您想编辑IdentityUser类,对吗
假设您已经在控制器中指定了值
首先,我们将IdentityUser类添加到ProfileModel类中
public class ProfileModel{
public IdentityUser User{get;set;}
//other profilemodel properties
}
在你看来:
<div class="form-group">
@Html.LabelFor(model => model.User.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User.Email, "", new { @class = "text-danger" })
</div>
</div>
您也应该在问题中显示视图。我添加了它,谢谢。将IdentityUser类添加到您的ProfileModel中Class@Josh你能证明你的意思吗?我想
ProfileModel driver = new ProfileModel();
//code that assigns values to each property
driver.email = "sample@sample.com";
return View("DriverProfile",driver);