标签:bs
Mvc在学习《Headfirst》的时候,就接触过了。对于这个庞大的模式。非常的感兴趣。Mvc就是设计模式的升华。using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using csdnblog.Models; namespace csdnblog.Controllers { public class HomeController : Controller { //创建一个数据集合(伪数据) List<Models.Dog> list = null; #region 0.1 初始化数据集合 +oid InitData() /// <summary> /// 初始化数据集合 /// </summary> public List<Models.Dog> InitData() { list = new List<Models.Dog>() { new Dog(){ID=1,Name="Dog1:xiaohei~"}, new Dog(){ID=2,Name="Dog2:xiaobai~"}, new Dog(){ID=3,Name="Dog3:xiaocai~"}, new Dog(){ID=4,Name="Dog4:xiaoyang~"}, }; return list; } #endregion public ActionResult Index() { System.Text.StringBuilder sbHtml = new System.Text.StringBuilder(4000); //2.1处理当前业务(比如读取数据库,判断等) //2.2创建一个数据集合(伪数据),获取数据 List<Models.Dog> list = InitData(); //2.1.2遍历集合,生成html代码,存入stringBuilder list.ForEach(d => { sbHtml.AppendLine("<div>" + d.ToString() + "</div>"); }); //2.2使用viewBag传输数据给 同名 index.cshtml 视图 //viewBag是一个dynamic类型集合,可以动态添加任意类型的任意名称的属性和值 ViewBag.HtmlStr = sbHtml.ToString(); //2.3加载同名视图index.cshtml return View(); } public ActionResult Index2() { return View(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace csdnblog.Models { public class Dog { public int ID { get; set; } public string Name { get; set; } public override string ToString() { return "ID=" + this.ID + ",Name=" + this.Name; } } }
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> @Html.Raw(ViewBag.HtmlStr) </div> </body> </html>
标签:bs
原文地址:http://blog.csdn.net/zc474235918/article/details/41422787