简体   繁体   English

如何从特定用户的数据库中检索值?

[英]How can I retrieve values from the database from a specific users?

I want to be able to retrieve values from the database from a specific user, which in this case @Model.user.Xp , it does not work, I just get 0. 我希望能够从特定用户从数据库中检索值,在这种情况下,该@Model.user.Xp无效,我只能得到0。

@model TheQuizR.Models.IndexViewModel

@using Microsoft.AspNet.Identity

<div class="row">
    <div class="col-sm-6 col-md-6">
        <ul class="list-group">
            <div class="blue">
                @User.Identity.GetUserName()<br />
        </div>
        <li class="list-group-item">
            Title
            <span class="badge">@Model.user.Xp</span>                                   
        </li>
        <li class="list-group-item">

In the IndexViewModel I have this: IndexViewModel我有这个:

public class IndexViewModel
{

    public ApplicationUser user = new ApplicationUser();

    public bool HasPassword { get; set; }
    public IList<UserLoginInfo> Logins { get; set; }
    public string PhoneNumber { get; set; }
    public bool TwoFactor { get; set; }
    public bool BrowserRemembered { get; set; }

}

In the ApplicationUser class I have all the properties: ApplicationUser类中,我具有所有属性:

public class ApplicationUser : IdentityUser
{
    [MaxLength(128)]
    public string Title { get; set; }
    [Range(0, 5000000)]
    public int Xp { get; set; }
    [Range(0, 100000)]
}

I cant get the id and the username thru Microsoft.AspNet.Identity (the one mark in yellow). 我无法通过Microsoft.AspNet.Identity(黄色的一个标记)获得ID和用户名。 I can't get all the other properties. 我无法获得所有其他属性。

1个

I would recommend to find the user in controller. 我建议在控制器中找到用户。 Then you can create another model or use Viewbag. 然后,您可以创建另一个模型或使用Viewbag。

string username = User.Identity.GetUserName();
var user = db.Users.First(u => u.UserAD == username);
ViewBag.userIDconnected = user.ID;

View - 查看-

<div class="row">
<div class="col-sm-6 col-md-6">
    <ul class="list-group">
        <div class="blue">
    </div>

    <li class="list-group-item">
        Title
        <span class="badge">@ViewBag.userIDconnected</span>                                   
    </li>
    <li class="list-group-item">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM