1

我的背景是:

public class RegistrationManagerContext : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()                
            .ToTable("aspnet_Users");
    }
}

我的用户类是:

[Table("aspnet_Users")]
public class User
{
    [Key]
    [Display(AutoGenerateField = true)]
    [Required]
    public Guid UserId { get; set; }

    [DataType("nvarchar")]
    [MaxLength(256)]
    [Required]
    public string UserName { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, ConvertEmptyStringToNull = true, DataFormatString = "{0:d}")]
    [Required]
    public DateTime LastActivityDate { get; set; }

    [NotMapped]
    public AccountProfile Profile
    { 
        get 
        { 
            if (_profile == null)
            {
                _profile = AccountProfile.GetUserProfile(this.UserName);
            }
            return _profile;
        }
    }
    private AccountProfile _profile = null;
}

我的调用方法(控制器)是:

namespace RegistrationManager.Controllers
{

    [Authorize(Roles="Admins")]
    public class AdminController : Controller
    {

        private RegistrationManagerContext db = new RegistrationManagerContext();

        //
        // GET: /Admin/

        public ActionResult Index(int? id)
        {
            var viewModel = db.Users;
            if (id.HasValue)
            {
                ViewBag.UserID = id.Value;
            }
            return View(viewModel);
        }

    }
}

我的看法是:

@model IEnumerable<RegistrationManager.Models.User>

@{
        ViewBag.Title = "Users";
}

<h2>Users</h2>


<table>
    <tr>
        <th>Email Address</th>
        <th>Given Names</th>
        <th>Surname</th>
        <th>Last Ac</th>
        <th>Refresh</th>
    </tr>

@foreach (var item in Model)
{
    string selectedRow = "";
    if (ViewBag.UserId != null && item.UserId == ViewBag.UserId)  
    {  
        selectedRow = "selectedrow";  
    }  
    <tr class="@selectedRow" valign="top">
        <td>
            @item.UserName
        </td>
        <td>
            @item.Profile.GivenNames
        </td>
        <td>
            @item.Profile.Surname
        </td>
        <td>
            @String.Format("{0:d}", item.LastActivityDate)
        </td>
        <td>
            @Html.ActionLink("Refresh", "Refresh", "Admin", new { id = item.UserId }) 
        </td>
    </tr>
}
</table>

那么我做错了什么或者我错过了什么?因为我没有数据!???肯定有数据,因为我至少应该看到我自己的登录信息,对吧!?

4

1 回答 1

1

你不应该做这样的事情:

    public ActionResult Index(Guid? id)
    {
        if (id.HasValue)
        {
            var viewModel = db.Users.Where(x=>x.UserId == id.Value).FirstOrDefault();
            //ViewBag.UserID = id.Value; //You do not need it here
            if(viewModel != null)
                  return View(viewModel);
        }
        return RedirectToAction("UsersListPage");
    }

更新#1

控制器:

    public ViewResult Index(){
           return View(db.Users.ToList());
    }

看法:

    @model IEnumerable<YourNamespace.User>

    @foreach (var user in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => user.UserName)
        </td>            
    </tr>
     }

这不适合你吗?

于 2012-12-13T07:33:31.257 回答