[英]ASP.NET MVC5 Displaying String in a TextBox
I have a partial view, which I am passing a model of type WebSite.Models.ManageModel
which is simply 我有一个局部视图,正在传递一个WebSite.Models.ManageModel
类型的模型,该模型很简单
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. 我想访问该电子邮件地址并将其放入我的局部视图中的TextBox
中。 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
. 现在,该电子邮件地址正确显示在label label-danger
,而不显示在我的TexBox
。 What is wrong with this code? 此代码有什么问题?
Thanks for your time. 谢谢你的时间。
Edit. 编辑。 The action that launches the Manage
view (which contains my partial views) is 启动“ 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();
}
The problem is, I don't know how to retrieve the users data. 问题是,我不知道如何检索用户数据。 My ManageUserViewModel
is 我的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; }
}
I know I can get the ApplicationUser
data via var user = UserManager.FindById(User.Identity.GetUserId());
我知道我可以通过var user = UserManager.FindById(User.Identity.GetUserId());
获得ApplicationUser
数据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. 但这是默认值加上我添加的EmailAddress
字段,因此它不包含密码信息。 What should I be doing in this case? 在这种情况下我该怎么办?
public class ApplicationUser : IdentityUser
{
[DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }
}
I don't think you can override the value like that without writing your own TextBoxFor
extension method. 我不认为无需编写自己的TextBoxFor
扩展方法就可以覆盖这样的值。 I believe the framework is simply ignoring your @value
. 我相信该框架只是忽略了您的@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. 尝试使用要显示的值填充视图模型( ManageUserViewModel.EmailAddress
)。 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. 显然,我不知道您要做什么,但是我不了解嵌套视图模型和UserEmailAddress
属性的UserEmailAddress
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.