博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net强类型视图
阅读量:7164 次
发布时间:2019-06-29

本文共 2152 字,大约阅读时间需要 7 分钟。

422101-20170519112226041-79580988.png

1.控制器 Controllers/StoreController.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using mvcDemo.Models; // 引入Modelsnamespace mvcDemo.Controllers{    public class StoreController : Controller    {        // GET: Store        public string Index()        {            return "Hello from Store.Index()";        }        // GET: Store        public string Browse(string genre)        {            string message = HttpUtility.HtmlEncode("Genre =" +genre);            return message;        }        // GET: Store        public string Details(int id)        {            string message = "Store.Details,ID = " + id;            return message;        }        public ActionResult List()        {            var albums = new List
(); for (int i = 0;i<10;i++) { albums.Add(new Album { Title = "Album "+ i}); } return View(albums); } }}

2.Models Models/Album.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace mvcDemo.Models{    public class Album    {        public virtual int AlbumId { get; set; }        public virtual int GenreId { get; set; }        public virtual int ArtistId { get; set; }        public virtual string Title { get; set; }        public virtual decimal Price { get; set; }        public virtual string AlbumArtUrl { get; set; }        public virtual Genre Genre { get; set; }        public virtual Artist Artist { get; set; }    }}

3.视图层 Views/Store/List.cshtml

@{    ViewBag.Title = "List";}@model IEnumerable
    @foreach (Album p in Model){
  • @p.Title
  • }

4.配置命名空间 Views/Web.config

点评,这里的传递要比ViewBag更加方便!

转载地址:http://pamwm.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
Spring学习总结(4)——Spring AOP教程
查看>>
Jenkins自动化构建(一)
查看>>
log4j配置详解
查看>>
我的友情链接
查看>>
面试100题之8
查看>>
一些以太网非核心功能的标准
查看>>
oracle EBS 克隆之数据库克隆
查看>>
【python2】commands模块getstatusoutput函数的小问题
查看>>
oracle常用sql
查看>>
iOS之隐藏状态栏
查看>>
Script:ASM修复脚本,寻找LISTHEAD和Kfed源数据
查看>>
mysql ERROR 1045 (28000): Access denied for user解决方法
查看>>
locale::facet::_S_create_c_locale name not valid
查看>>
shell 中的常用技巧
查看>>
25个增强iOS应用程序性能的提示和技巧--中级篇
查看>>
java反射机制
查看>>
Quartz结合SPRING多任务定时调用
查看>>
Twitter的API使用方法
查看>>
inotify实时监控程序安装
查看>>