我在UserProfile中添加了一些额外的字段,并希望在_LoginPartial.cshtml页面中显示First和Last Name字段。有人可以帮助我。
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class RegisterModel
{
[Required]
[RegularExpression(@"[\w-]+@([\w-]+\.)+[\w-]+", ErrorMessage = "Enter a valid email address")]
[Display(Name = "User name")]
public string UserName { get; set; }
[DataType(DataType.EmailAddress)]
[RegularExpression(@"[\w-]+@([\w-]+\.)+[\w-]+", ErrorMessage = "not a valid email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[RegularExpression("([a-zA-Z0-9 .&'-]+)", ErrorMessage = "Enter only alphabets and numbers of First Name")]
[Display(Name = "First name")]
public string FirstName { get; set; }
[Display(Name = "Last name")]
public string LastName { get; set; }
}
_LoginPartial.chtml
@if (Request.IsAuthenticated)
{
<text>
<font color="white">
@*Hello,</font> @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!*@
Hello,</font> @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
</text>
}
else
{
<ul>
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
以下是我正在处理的网站:
答案 0 :(得分:4)
1-首先将此操作添加到帐户控制器:
[ChildActionOnly]
public PartialViewResult GetUserInfo()
{
if (User.Identity.IsAuthenticated)
{
var context = new BlogDBEntities();
var id = WebSecurity.CurrentUserId;
var user = context.UserProfiles.SingleOrDefault(u => u.UserId == id);
return PartialView(user);
}
return PartialView();
}
2-为此操作创建partilaview:GetUserInfo.cshtml
@model MVCBlog.Models.UserProfile
@if (Model != null)
{
@Model.RealName
}
3-修改_loginPartial.cshtml如下:
@if (Request.IsAuthenticated) {
<text>
Hello, @Html.ActionLink(Html.Action("GetUserInfo", "Account").ToString(), "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
</text>
} else {
<ul>
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
答案 1 :(得分:0)
我强烈推荐你
[Datatype.Email]
[Display(Name = "User name")]
public string UserName { get; set; }
而不是你现在拥有的东西。