diff --git "a/\346\254\247\351\230\263\344\271\276/20241122.md" "b/\346\254\247\351\230\263\344\271\276/20241122.md" new file mode 100644 index 0000000000000000000000000000000000000000..33474b38eb6f4707336d46935a7172483dfd5313 --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241122.md" @@ -0,0 +1,84 @@ +# 渲染复杂数据到页面 +模型 (Models/ComplexData.cs) +public class ComplexData +{ + public string Name { get; set; } + public int Age { get; set; } + public List Hobbies { get; set; } +} +控制器 (Controllers/HomeController.cs) +using System.Collections.Generic; +using System.Web.Mvc; +using YourNamespace.Models; + +public class HomeController : Controller +{ + public ActionResult ComplexData() + { + var model = new ComplexData + { + Name = "John Doe", + Age = 30, + Hobbies = new List { "Reading", "Traveling", "Swimming" } + }; + return View(model); + } +} +视图 (Views/Home/ComplexData.cshtml) +@model YourNamespace.Models.ComplexData + +

Name: @Model.Name

+

Age: @Model.Age

+

Hobbies:

+ +3. 渲染集合数据到页面 +模型 (Models/Item.cs) +public class Item +{ + public string Name { get; set; } + public double Price { get; set; } +} +控制器 (Controllers/HomeController.cs) +using System.Collections.Generic; +using System.Web.Mvc; +using YourNamespace.Models; + +public class HomeController : Controller +{ + public ActionResult CollectionData() + { + var items = new List + { + new Item { Name = "Apple", Price = 0.99 }, + new Item { Name = "Banana", Price = 0.59 }, + new Item { Name = "Cherry", Price = 2.99 } + }; + return View(items); + } +} +视图 (Views/Home/CollectionData.cshtml) +@model IEnumerable + +

Items

+ + + + + + + + + @foreach (var item in Model) + { + + + + + } + +
NamePrice
@item.Name$@item.Price
\ No newline at end of file