0

I have a partial view, which I am passing a model of type WebSite.Models.ManageModel which is simply

public class ManageModel
{
    public string UserEmailAddress { get; set; }
    public ManageUserViewModel ManageUserViewModel { get; set; }
}

I want to access the email address and place it into a TextBox in my partial view. So in that partial view I have

<div class="input-group">
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.ManageUserViewModel.EmailAddress, 
            new 
            { 
                @class = "col-md-10 form-control", 
                @type = "text",
                @placeholder = "Email Address",
                @value = Model.UserEmailAddress
             })
    </div>
</div>
<span class="label label-danger">@Model.UserEmailAddress</span>

Now, the email address correctly displays in the label label-danger but not in my TexBox. What is wrong with this code?

State

Thanks for your time.


Edit. The action that launches the Manage view (which contains my partial views) is

// GET: /Account/Manage.
public ActionResult Manage(ManageMessageId? message)
{
    ViewBag.StatusMessage =
        message == ManageMessageId.ChangePasswordSuccess ? 
            "Your password has been changed": 
        message == ManageMessageId.SetPasswordSuccess ? 
            "Your password has been set" : 
        message == ManageMessageId.RemoveLoginSuccess ? 
            "The external login was removed" : 
        message == ManageMessageId.ChangeEmailAddressSuccess ?
            "Your email address was successfully updated" :
        message == ManageMessageId.Error ? 
            "An error has occurred." : "";
    ViewBag.HasLocalPassword = HasPassword();
    ViewBag.ReturnUrl = Url.Action("Manage");

    // Not passing the model to the view, because I am 
    // unsure how to retrieve the information.
    return View(); 
}

The problem is, I don't know how to retrieve the users data. My ManageUserViewModel is

public class ManageUserViewModel
{
    [Required]
    [EmailAddress(ErrorMessage = "Invalid email address.")]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string EmailAddress { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Current password")]
    public string OldPassword { get; set; }

    [Required]
    [StringLength(100, ErrorMessage =
        "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New password")]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm new password")]
    [Compare("NewPassword", ErrorMessage =
        "The new password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

I know I can get the ApplicationUser data via var user = UserManager.FindById(User.Identity.GetUserId()); but this is the default plus my added EmailAddress field, so it does not contain the password information. What should I be doing in this case?

public class ApplicationUser : IdentityUser
{
    [DataType(DataType.EmailAddress)]
    public string EmailAddress { get; set; }
}
4
  • Your TextBoxFor should be TextBoxFor(m => m.UserEmailAddress... Commented Nov 3, 2014 at 9:36
  • I had something similar if i remember correctly. Try capital case the v in value. @Value = Model.UserEmailAddress Commented Nov 3, 2014 at 10:17
  • FYI, if you are actually able to retrieve a user's password, you're doing something wrong. Do a Google search for password hashing and how to store passwords properly. Commented Nov 4, 2014 at 0:41
  • Thanks @Ic. I am hashing it... Commented Nov 4, 2014 at 9:35

1 Answer 1

2

I don't think you can override the value like that without writing your own TextBoxFor extension method. I believe the framework is simply ignoring your @value. Furthermore it seems like your design pattern is a little off, and you have somewhat of a hybrid model where the same information is in two different places.

Try populating your view model (ManageUserViewModel.EmailAddress) with the value you want displayed. Obviously I don't know what you're trying to do, but I don't understand the purpose of the nested view model and the UserEmailAddress property.

Sign up to request clarification or add additional context in comments.

9 Comments

And there is no point in @type = "text" since TextBoxFor() renders it anyway
@StephenMuecke Agreed, although not sure why, but I wondered if the OP is again trying to override a type=email if there's an attribute attached, so I left it alone.
Hi guys, thanks for the response. I will look to use ManageUserViewModel to get the email, but at the moment I am just playing about trying to learn ASP.NET/MVC. @StephenMuecke how can I populate the text box with the required value if I can't do it as I have attempted?
@Html.TextBoxFor() will always render type="text". Its only @Html.EditorFor() that will consider the datatype attributes and render email, date etc
The whole purpose of using the strongly typed helpers is to ensure you properties are correctly bound which is why it always renders the name and value attributes based on the model properties. You can use @TextBox("ManageUserViewModel.EmailAddress", Model.UserEmailAddress, new {... but I do not recommend it. Just set the value of ManageUserViewModel.EmailAddress to the value of UserEmailAddress in the controller before you pass the view. Your using MVC, make use of its features!
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.