【问题标题】:How to bind c# variable to HTML如何将 c# 变量绑定到 HTML
【发布时间】:2016-10-28 22:01:32
【问题描述】:

我已经从控制器中的 SQL 查询创建了一个 DataTable。

如何将“#example”与“categories”“联系起来”?

EDIT1:建议使用 jQuery.DataTables

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Employee Index Page</title>
</head>
<body>
    <div>
    <h1>Employee Index Page</h1>
        <table id="example">

</div>
</body>
</html>

@{ var categories = (System.Data.DataTable)ViewData["MyData"]; }

<script>
    $(document).ready(function () {
        $('#example').DataTable();
    });
</script>

原创

I want to assign this to a gridview. The Code from the View is below but GridView1 needs to be defined somehow.

In the c# categories has the right contents, but GridView1 get the error "does not exist in the current context".

How and where do I fix that?

    @{
        Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Employee Index Page</title>
    </head>
    <body>
        <div>
        <h1>Employee Index Page</h1>
            <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                          runat="server" DataSource='<%# GetData() %>' AutoGenerateColumns="true">
            </asp:GridView>
            @{ 
                var categories = (System.Data.DataTable)ViewData["MyData"];
                GridView1.DataSource = categories;
                GridView1.DataBind();
            }
        </div>
    </body>
    </html>

【问题讨论】:

  • 你为什么在 MVC 中使用 asp:gridview?
  • 网络搜索表明这是一种显示数据表的简单方法。我对替代品持开放态度!
  • 就个人而言,我会考虑将数据表(或类似的)与 MVC codeproject.com/Articles/155422/…
  • 你不能混合 ASP 控件和 MVC 我很确定(老实说我从没想过尝试......)
  • 你可以做一个普通的 HTML &lt;table&gt; 和 for-loop 遍历所有的标题、列、行等......

标签: c# jquery asp.net-mvc


【解决方案1】:

首先,我建议您不要将所有内容都放在一个视图中,为网格创建视图以 @model List&lt;YourClass&gt; 和视图为您的布局。

在 MVC 中显示 GridView,有很多选项(免费和非免费)

数据表示例

your Html for example
---------------------
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
            </tr>
        </thead>
        <tbody>
            @foreach(var item in Model)
            <tr>
                <td>@item.Name</td>
                <td>@item.Position</td>
            </tr>
         <tbody>
    </table>


<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.12.3.js"></script> 
<script type="text/javascript" charset="utf8"  src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
 <script> 
      $(document).ready(function () { 
         $('#example').DataTable(); 
       }); 
 </script> 

【讨论】:

  • 艾哈迈德,我已对问题进行了更改 - 如何将 jquery.datatable 与我的数据表连接?
  • 你需要先了解更多关于MVC和javascript的知识
  • 这是我在使用 MVC、javscript、Ajax 之前创建的一个开源项目,我认为它会有所帮助github.com/AhmedRagheb/Movies-Search
  • @ManInMoon 我用如何使用 DataTable 的例子来编辑我的答案
  • 艾哈迈德,感谢您提供上面的代码。我很困惑 - 为什么在使用 JQuery.Datatable 时需要循环并创建行?我还不如只写HTML?我认为 Jquery 的重点是让它更简单?
猜你喜欢
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
相关资源
最近更新 更多