0

我一直在尝试列出数据库中的所有用户,但我很难。我是 MVC3 的新手,请帮忙。:)

这是我的用户列表

using System;
using System.Data.Entity;
using System.Collections.Generic;
 using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MVCDemo.Models.DB;
using MVCDemo.Models.ViewModel;

namespace MVCDemo.Models.ViewModel
{


    public class UserList
    {

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string ContactNumber { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

     }

    public class Users
    {
        private DeveloperReportEntities dre = new DeveloperReportEntities();
        public UserList _userList = new UserList();


        public Users()
        {
            DB.Users_Tbl userTBL = new DB.Users_Tbl();
            _userList.FirstName = userTBL.FirstName;
            _userList.LastName = userTBL.LastName;
            _userList.Email = userTBL.Email;
            _userList.Address = userTBL.Address;
            _userList.ContactNumber = userTBL.ContactNo;
            _userList.Username = userTBL.Username;
            _userList.Password = userTBL.Password;
         }


    }
}

应该通过这里控制:AdminController

    public class AdminController : Controller
    {
        //
         // GET: /Admin/

        public ActionResult AdminHomePage()
        {
             return View();
        }

        public ActionResult ViewAll()
         {
            return View();
         }
     }   

在我的解决方案资源管理器中,模型文件夹中有一个 samplemodel.edmx

4

1 回答 1

0

首先,该类Users应该封装一个List. User它应该是这样的:

/Controllers/Admin/AdminController.cs

public class User
{
   public string FirstName { get; set; }
   public string LastName { get; set }
}

public class AdminController : Controller
{
    //
    // GET: /Admin/
    public ActionResult AdminHomePage()
    {
         return View();
    }

    public ActionResult ViewAll()
    {
        var userTBL = new DB.Users_Tbl();
        var users = userTBL.Select(u => new User { FirstName = u.FirstName, LastName = u.LastName };
        return View(users);
    }
 }

/Views/Admin/ViewAll.cshtml

@model List<User>
<ul>
@foreach(var user in Model)
{
   <li>@user.FirstName @user.LastName</li>
}
</ul>
于 2013-02-23T05:05:40.713 回答