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>




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。