1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
理想情況下,您應(yīng)該將其加載到您的模型中:
public ActionResult Index()
{
FulfillmentContext db = new FulfillmentContext();
string username =
HttpContext.Current.User.Identity.GetUserName();
var currentUser = (from u in db.AspNetUsers
join ur in db.AspNetUserRoles on u.Id equals ur.UserId
join r in db.AspNetRoles on ur.RoleId equals r.Id
where u.UserName == username
select u).First();
var model = new Model();
model.CurrentUser = currentUser;
return View(model);
}
但是,您可以創(chuàng)建一個(gè) HTML 幫助程序擴(kuò)展:
public static AspNetUser GetCurrentUser(this HtmlHelper helper)
{
FulfillmentContext db = new FulfillmentContext();
string username = HttpContext.Current.User.Identity.GetUserName();
var currentUser = (from u in db.AspNetUsers
join ur in db.AspNetUserRoles on u.Id equals ur.UserId
join r in db.AspNetRoles on ur.RoleId equals r.Id
where u.UserName == username
select u).First();
return currentUser;
}
然后從您的視圖中調(diào)用它:
@{
var user = Html.GetCurrentUser();
}
- 1 回答
- 0 關(guān)注
- 101 瀏覽
添加回答
舉報(bào)