WebGrid正在打印一个空表
任何人都可以看到为什么这个简单的测试网站打印出一个空表(在teh html,一个表是用2行(tr)创建的,但没有生成的单元格(td))。
或者如何调试它的最佳方式
DebugController.cs …
using System; using System.Collections.Generic; using System.Web; using System.Web.Mvc; namespace myproject.Controllers { public class type1 { public String ipAddress; public String connectionId; } public ActionResult Debug2(string arg1) { List obj1 = new List(); obj1.Add(new type1 {ipAddress="192.168.1.1", connectionId="123"}); obj1.Add(new type1 {ipAddress="192.168.1.2", connectionId="345"}); ViewBag.obj1 = obj1; return View(); } } }
和Debug2.cshtml ….
@{ Layout = null; } @{ var grid = new WebGrid(ViewBag.obj1); } @grid.GetHtml()
我可能会尝试更强烈的类型。
同样IIRC你必须使用你的模型上的属性而不是字段,以便WebGrid助手可以选择它们:
public class Type1 { public string IPAddress { get; set; } public string ConnectionId { get; set; } }
在你的行动中:
public ActionResult Debug2(string arg1) { var model = new List(); model.Add(new Type1 { IPAddress = "192.168.1.1", ConnectionId = "123" }); model.Add(new Type1 { IPAddress = "192.168.1.2", ConnectionId = "345" }); return View(model); }
最后在你的强类型视图中:
@model IList @{ Layout = null; } @{ var grid = new WebGrid(Model); } @grid.GetHtml()