如何使用MVC4和剃须刀视图在LoginPartial页面中显示自定义字段
问题描述:
我在UserProfile中添加了一些额外的字段,并且想在_LoginPartial.cshtml页面中显示First and Last Name字段。有人可以帮帮我吗。如何使用MVC4和剃须刀视图在LoginPartial页面中显示自定义字段
[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-][email protected]([\w-]+\.)+[\w-]+", ErrorMessage = "Enter a valid email address")]
[Display(Name = "User name")]
public string UserName { get; set; }
[DataType(DataType.EmailAddress)]
[RegularExpression(@"[\w-][email protected]([\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>
}
这里是网站我的工作:
答
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>
}
答
我会强烈建议您
[Datatype.Email]
[Display(Name = "User name")]
public string UserName { get; set; }
代替你目前有什么。
问题在哪里? – Mairaj 2014-09-02 11:59:38