ASP.NET MVC5在TextBox中显示字符串

时间:2014-11-03 09:30:09

标签: c# asp.net asp.net-mvc twitter-bootstrap-3 asp.net-mvc-5

我有一个局部视图,我传递的是WebSite.Models.ManageModel类型的模型,它只是

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

我想访问该电子邮件地址,并在部分视图中将其放入TextBox。所以在那个局部视图中我有

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

现在,电子邮件地址正确显示在label label-danger中,但未显示在我的TexBox中。这段代码有什么问题?

State

感谢您的时间。


编辑。启动Manage视图(包含我的部分视图)的操作是

// 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(); 
}

问题是,我不知道如何检索用户数据。我的ManageUserViewModel

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

我知道我可以通过ApplicationUser获取var user = UserManager.FindById(User.Identity.GetUserId());数据,但这是默认加上我添加的EmailAddress字段,因此它不包含密码信息。在这种情况下我该怎么做?

public class ApplicationUser : IdentityUser
{
    [DataType(DataType.EmailAddress)]
    public string EmailAddress { get; set; }
}

1 个答案:

答案 0 :(得分:2)

我认为如果不编写自己的TextBoxFor扩展方法,就可以覆盖这样的值。我相信框架只是忽略了你的@value。此外,您的设计模式似乎有些偏差,并且您有一些混合模型,其中相同的信息位于两个不同的位置。

尝试使用您想要显示的值填充视图模型(ManageUserViewModel.EmailAddress)。显然,我不知道你要做什么,但我不了解嵌套视图模型和UserEmailAddress属性的用途。