显示项目列表。在我的情况下,用户列表

时间:2013-02-23 02:55:18

标签: asp.net asp.net-mvc-3 list details

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

这是我的UserList

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

1 个答案:

答案 0 :(得分: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>