码迷,mamicode.com
首页 > Web开发 > 详细

MVC普通数据展示及,强类型数据展示

时间:2014-08-27 10:56:17      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:mvc强类型数据展示

《1》

Model 添加了一个UserInfo类    UserInfo.cs

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

namespace MvcApplication1.Models
{
    public class UserInfo
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string Gender { get;set; }
    }
}


控制器 HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

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

        public ActionResult Index()
        {

            return View();
        }

        public ActionResult UserInfo()
        {
            List<UserInfo> userinfo = new List<UserInfo>()
            {
                new UserInfo(){ Name="小w王", Age=18, Gender="女"},
                new UserInfo(){ Name = "小李", Age = 19, Gender = "男"} 
            };
            ViewData["userinfo"] = userinfo;
            return View();
        }

    }
}

视图:UserInfo.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!--这里是引入UserInfo类所在的的MvcApplication1.Models名称空间-->
<%@ Import Namespace="MvcApplication1.Models" %>
<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>UserInfo</title>
</head>
<body>
    <div>
          <table>
        <tr><th>姓名</th><th>年龄</th><th>性别</th></tr>
      <% List<UserInfo> userinfo = ViewData["UserInfo"] as List<UserInfo>;
             foreach(var v in userinfo)
             { %>
                <tr><td><%:v.Name %> </td><td><%:v.Age %></td><td><%:v.Gender %></td></tr>
                
            <% }  %> 
                           
      </table>  
    </div>
</body>
</html>
bubuko.com,布布扣




MVC普通数据展示及,强类型数据展示

标签:mvc强类型数据展示

原文地址:http://blog.csdn.net/fanbin168/article/details/38866313

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!