редактировать вид профиля asp. net -mvc5 - PullRequest
0 голосов
/ 23 апреля 2020

Я новичок в asp. net mvc5, и в моем приложении я хочу получить информацию о пользователе в их профиле. В профиле я пытаюсь отобразить информацию о пользователе и разрешить им редактировать свою информацию. Но когда я запускаю программу, я получаю окно редактирования и отображения пустым. Я ожидаю увидеть предыдущую информацию пользователя, когда он пытается редактировать, например. вот изображение того, что я хочу: enter image description here

это мое действие:

       [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>
}

1 Ответ

1 голос
/ 23 апреля 2020

Если я правильно понял вопрос, вы хотите отредактировать класс IdentityUser правильно?

Учитывая, что вы уже присваиваете значения в вашем контроллере.

Сначала мы добавим класс IdentityUser к вашему ProfileModel Class

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>

или, если вы просто хотите показать некоторые значения при редактировании, вам нужно присвоить значение вашей ProfileModel

Контроллер:

ProfileModel driver = new ProfileModel();
//code that assigns values to each property
driver.email = "sample@sample.com";
return View("DriverProfile",driver);
...