WebGrid正在打印出一个空表 [英] WebGrid is printing out an empty table

查看:55
本文介绍了WebGrid正在打印出一个空表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能看到为什么这个简单的测试网站会打印出一个空表(在html中,创建的表有2行(tr),但没有生成任何单元格(td)).

Can anyone see why this simple test website is printing out an empty table (in teh html, a table is getting created with 2 rows (tr) but there are no cells (td) getting produced).

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

Or how is the best way to go about debugging it

DebugController.cs ...

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<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 ....

And 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>

推荐答案

我可能会尝试更强类型的输入.

I'd probably try something a bit more strongly typed.

此外,IIRC您还必须在模型上使用属性,而不是在字段上使用属性,以便WebGrid帮助器可以选择它们:

Also IIRC you must use properties on your model instead of fields so that the WebGrid helper could pick them up:

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);
}

最后进入您的强类型视图:

and finally in your strongly typed view:

@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>

这篇关于WebGrid正在打印出一个空表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆