日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC3数据绑定到VIEW的方式

發布時間:2023/11/29 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC3数据绑定到VIEW的方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ASP.NET MVC3數據綁定到VIEW的方式

1、???? 指定頁面數據的強類型Module

數據類型是強類型,編譯時報錯,運行效率高

Action:

????? public ActionResult Index()

????? {

??????? var _instructors = new List<Instructor>(

?????????? new Instructor[]

?????????? {

????????????? new Instructor

????????????? {

???????????????? Name = "Nimane1",

???????????????? TwitterHandler = "@fasdd",

???????????????? HtmlDescription = "This"

????????????? }

????????????? , new Instructor

????????????? {

???????????????? Name = "Nimane2",

???????????????? TwitterHandler = "@fasdd",

???????????????? HtmlDescription = "This"

????????????? }

?????????? }

??????? );

??????? ViewBag.Message = "Welcome to ASP.NET MVC!";

??????? return this.View(_instructors);

????? }

View:

@using MvcApplication1.Models

@model IEnumerable<Instructor>

@{

? ??ViewBag.Title = "Index";

??? Layout = "~/Views/Shared/_Layout.cshtml";

}

<h2>@ViewBag.Message</h2>

<div>

??? @foreach (var item in Model)

??? {

??????? @Html.Partial("InstructorView", item)

??? }

</div>

InstructorView:

@model MvcApplication1.Models.Instructor

<fieldset>

??? <legend>Instructor</legend>

??? <div class="display-label">

??????? Name</div>

??? <div class="display-field">

??????? @Model.Name

??? </div>

??? <div class="display-label">

??????? TwitterHandler</div>

??? <div class="display-field">

??????? @Model.TwitterHandler

??? </div>

??? <div class="display-label">

??????? HtmlDescription</div>

??? <div class="display-field">

??????? @Html.Raw(@Model.HtmlDescription)</div>

</fieldset>

<p>

??? @Html.ActionLink("Edit", "Edit", new { })

??? @Html.ActionLink("Back to list", "Index")

</p>

2、???? 使用ViewData綁定到頁面

數據類型是object,運行時報錯,在 頁面中需要對數據進行顯示轉換,運行效率低

Action:

????? public ActionResult ViewData ()

????? {

??????? List<string> colors = new List<string>(

?????????? new string[]

?????????? {

????????????? "red","green","blue"

?????????? }

??????? );

?

??????? ViewData["listColors"] = colors;

??????? ViewData["dateNow"] = DateTime.Now;

??????? ViewData["name"] = "Nicoles";

??????? ViewData["age"] = 24;

?

??????? return this.View();

????? }

View:

??? <div>

??????? <p>section for viewdata display:</p>

??????? <p>my name is :

??????????? <b>@ViewData["name"]</b>

??????????? <b>@ViewData["age"]</b> years old.

??????????? <br/>

??????????? I like the colors:

??????? </p>

??????? <ul id="colors">

??????????? @foreach (var color in ViewData["listColors"] as List<string>)

??????????? {

??????????????? <li>

??????????????????? <font color="@color">@color</font>

??????????????? </li>

??????????? }

??????????? <li></li>

??????? </ul>

??? </div>

3、???? 使用ViewBag綁定到頁面

數據類型是dynamic,運行時報錯,運行效率中

????? public ActionResult ViewBag()

????? {

??????? List<string> colors = new List<string>(

?????????? new string[]

?????????? {

????????????? "red","green","blue"

?????????? }

??????? );

??????? ViewBag.ListColors = colors;

??????? ViewBag.DateNow = DateTime.Now;

??????? ViewBag.Name = "Nicoles";

??????? ViewBag.Age = 24;

??????? return this.View();

????? }

View:

<div>

??????? <p>section for viewbag display:</p>

??????? <p>my name is :

??????????? <b>@ViewBag.Name</b>

??????????? <b>@ViewBag.Age</b> years old.

??????????? <br/>

??????????? I like the colors:

??????? </p>

??????? <ul id="colors_a">

??????????? @foreach (var color in @ViewBag.ListColors)

??????????? {

??????????????? <li>

??????????????????? <font color="@color">@color</font>

??????????????? </li>

??????????? }

??????????? <li></li>

??????? </ul>

??? </div>

?

?

總結

以上是生活随笔為你收集整理的ASP.NET MVC3数据绑定到VIEW的方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。