1

谁能明白为什么这个简单的测试网站打印出一个空表(在 html 中,创建了一个包含 2 行 (tr) 的表,但没有生成单元格 (td))。

或者调试它的最佳方法是什么

调试控制器.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<dynamic> obj1 = new List<dynamic>();
            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;
}   
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="~/Scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="~/Scripts/jquery.signalR-1.1.3.js"></script>
    <script type="text/javascript" src="~/signalr/hubs"></script>
    @{
        var grid = new WebGrid(ViewBag.obj1);
    }
</head>
<body>
    <div>
        @grid.GetHtml()
    </div>
</body>
</html>
4

1 回答 1

1

我可能会尝试一些更强大的类型。

此外 IIRC,您必须在模型上使用属性而不是字段,以便 WebGrid 助手可以获取它们:

public class Type1
{
    public string IPAddress { get; set; }            
    public string ConnectionId { get; set; }
}

在你的行动中:

public ActionResult Debug2(string arg1)
{
    var model = new List<Type1>();
    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<Type1>
@{
    Layout = null;
}   
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="~/Scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="~/Scripts/jquery.signalR-1.1.3.js"></script>
    <script type="text/javascript" src="~/signalr/hubs"></script>
    @{
        var grid = new WebGrid(Model);
    }
</head>
<body>
    <div>
        @grid.GetHtml()
    </div>
</body>
</html>
于 2013-08-21T22:03:00.133 回答