MVC--概述、列表显示数据

 

运行机制:

 列表显示数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mvc1.Models
{
    public class UsersData
    {
        DataClasses1DataContext conn = new DataClasses1DataContext();

        //public UsersData()
        //{
 
        //}

        public List<Users> Select()
        {
            return conn.Users.ToList();
        }
    }
}
Models
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="Mvc1.Models" %>
<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table style="background-color:navy;width:100%;text-align:center;">
        <tr style="color:white;">
            <td>用户名</td>
            <td>密码</td>
            <td>昵称</td>
            <td>性别</td>
            <td>生日</td>
            <td>民族</td>
            <td>地区</td>
        </tr>

        <%
            List<Users> list = new UsersData().Select();
            foreach(Users u in list)
            {
        %>

        <tr style="background-color:white;">
            <td><%=u.username %></td>
            <td><%=u.password %></td>
            <td><%=u.nickname %></td>
            <td><%=u.gender?"":"" %></td>
            <td><%=u.birthday.ToString("yyyy-MM-dd") %></td>
            <td><%=u.Nation1.Name %></td>
            <td><%=u.area %></td>
        </tr>
        <%
            } 
        %>
    </table>
</body>
</html>
Views
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc1.Models;

namespace Mvc1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
    }
}
Controllers

 

posted @ 2016-11-09 17:05  庚xiao午  阅读(1895)  评论(2编辑  收藏  举报