1.控制器 Controllers/StoreController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvcDemo.Models; // 引入Models namespace 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<Album>();
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<Album>
<ul>
@foreach (Album p in Model)
{
<li>@p.Title</li>
}
</ul>

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

<?xml version="1.0"?>

<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections> <system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="mvcDemo.Models" /> <!-- 增加命名空间 -->
<add namespace="mvcDemo" />
</namespaces>
</pages>
</system.web.webPages.razor> <appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings> <system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer> <system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
</configuration>

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

.Net强类型视图的更多相关文章

  1. MVC强类型视图、强类型HTML辅助方法

    强类型视图: <table> <tr> <td>ID:</td><td><%: Model.Id %></td> & ...

  2. MVC 强类型视图

    MVC里面的强类型视图的确是一个很强大的东西,结合EF(Entity Framework)的话更加强大,可以直接把你的数据库直接生成强视图的增删查改的视图,在视图中所有Model的属性均动态的,我们不 ...

  3. asp.net mvc 强类型视图中传入List 数据到控制器

    问题来源: 在和一位技术老师聊天时,老师问我一个mvc 表单提交的问题,问道:怎样在表单提交的时候,将 带有 List 属性的对象传入控制器? 这时,我有点呆了,以前一直都好像是 单一属性的表单提交, ...

  4. MVC强类型视图,详细信息展示【五】

    一.在MVC三种讲到两种后端发送数据到前端的方法,今天讲的是第三种,发送过去的方法 [强类型视图]. 1. 强类型视图,就是指在传递的过程中只能传递一种类型的数据到该视图中,就比如默认的传递的数据类型 ...

  5. MVC 5 Strongly Typed Views(强类型视图)

    学习MVC这样久以来,发觉网站上很多MVC的视频或是文章,均是使用Strongly Type views来实现控制器与视图的交互.Insus.NET以前发布的博文中,也大量使用这种方式: <Da ...

  6. MVC 创建强类型视图

    •在ViewModel中创建一个类型 •在Action中为ViewData.Model赋值 •在View中使用"@model类型"设置 14 手动创建强类型视图 •在ViewMod ...

  7. asp.net core mvc视频A:笔记3-3.Model与强类型视图

    创建项目,添加TestController 定义一个类(Model),并赋值 建立视图页面 注意:Model是一个特定的对象,取决于传递过来的参数 运行结果 到现在为止,依然没有改变动态类型,需要转换 ...

  8. 使用mvc时,在视图view中使用强类型视图,在web.config文件中添加命名空间namespace的引用不起作用,解决方法

    这是view中的model代码: @model t_user_info 这是web.config配置文件只的代码: <namespaces> <add namespace=" ...

  9. Razor强类型视图下的文件上传

    域模型Users.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; namesp ...

随机推荐

  1. NOIP2017 小凯的疑惑 解题报告(赛瓦维斯特定理)

    题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小 凯想知道在无法准确支付的物品中,最贵的 ...

  2. Hive框架基础(二)

    * Hive框架基础(二) 我们继续讨论hive框架 * Hive的外部表与内部表 内部表:hive默认创建的是内部表 例如: create table table001 (name string , ...

  3. fixed说明

    http://blog.163.com/hc_ranxu/blog/static/367231822013102265151785/

  4. <Sicily>Catch the thief

    一.题目描述 A thief has robbed a bank in city 1 and he wants to go to city N. The police know that the th ...

  5. webpack 读取文件变量

    1.webpack 获取文件中变量 <%= htmlWebpackPlugin.options.title %> <p><%= htmlWebpackPlugin.opt ...

  6. zookeeper启动闪退

    编辑zkServer.cmd在它的尾行加上 pause      就可以将闪退停住来观察闪退的原因. 遇到Exception in thread "main" java.lang. ...

  7. php八大设计模式之适配器模式

    将一个抽象被具体后的结果转换成另外一个需求所需的格式. 在生活中也处处有适配器的出现,比如转换头,就是让两种不同的规格合适的搭配在一起. <?php header("content-t ...

  8. U-boot 启动内核

    1:什么是UBOOT,为什么要有UBOOT? UBOOT的主要作用是用来启动linux内核,因为CPU不能直接从块设备中执行代码,需要把块设备中的程序复制到内存中,而复制之前还需要进行很多初始化工作, ...

  9. HDU——T 2647 Reward

    http://acm.hdu.edu.cn/showproblem.php?pid=2647 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  10. 华为:一张图看懂 HBase

    来自为知笔记(Wiz)