0

I have created a default ASP.NET MVC 5 project.

I have added a FullName property to the user:

public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }
}

Added this property to the viewmodel as well:

public class ManageUserViewModel
{
    [Display(Name = "Name")]
    public string FullName { get; set; }

    ...
}

And display a textbox on a view:

@Html.TextBoxFor(m => m.FullName)

It works well, I can set the FullName of the user. I have only one problem: if the user already has a FullName set, the textbox is still empty. How can I make it have to actual value of FullName property?

EDIT:

This is the controller which displays the view with the textbox:

    public ActionResult Manage(ManageMessageId? message)
    {
        ViewBag.StatusMessage =
            message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
            : message == ManageMessageId.ChangeDetailsSuccess ? "Your details have been changed."
            : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
            : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
            : message == ManageMessageId.Error ? "An error has occurred."
            : "";
        ViewBag.HasLocalPassword = HasPassword();
        ViewBag.ReturnUrl = Url.Action("Manage");
        return View();
    }
5
  • Do you map it properly in the controller when returning the view? You have to grab the FullName from the ApplicationUser and put it in the view model.
    – JLe
    Nov 24, 2013 at 18:02
  • set a breakpoint in the .cshtml and check the FullName property of the Model
    – Florian
    Nov 24, 2013 at 18:06
  • @JLe: I'm afraid I don't. How can I do that? I have added the controller method which returns the view which has the textbox.
    – Adam Szabo
    Nov 24, 2013 at 18:08
  • @thefiloe: after setting the breakpoint, how can I access the model while debugging the cshtml?
    – Adam Szabo
    Nov 24, 2013 at 18:11
  • Just use the DirectInput-Window and type Model.FullName
    – Florian
    Nov 24, 2013 at 20:56

1 Answer 1

1

You need to 1) get the ApplicationUser you're looking for, and 2) bind the property to the view model.

public ActionResult Manage(ManageMessageId? message)
{
    ViewBag.StatusMessage =
        message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
        : message == ManageMessageId.ChangeDetailsSuccess ? "Your details have been changed."
        : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
        : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
        : message == ManageMessageId.Error ? "An error has occurred."
        : "";
    ViewBag.HasLocalPassword = HasPassword();
    ViewBag.ReturnUrl = Url.Action("Manage");

    // Get the currently logged on user
    var user = UserManager.FindById(User.Identity.GetUserId());

    // Copy the value of FullName to the view model
    var vm = new ManageUserViewModel {
        FullName = user.FullName
    };
    return View(vm);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.