我正在try 用ASP.NETMVC创建带有HTML显示的表格.

这是样品

@model IEnumerable
<Vitashopper.Models.Goods>

  <title>
    Master Goods Vitashopper
  </title>
  
  <p>
    @Html.ActionLink("Create New", "Create", new { @class = "btn btn-success" })
  </p>

  <table class="table">
    <tr>
      <th>
        @Html.DisplayNameFor(model => model.ProductId)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.ProductName)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.BuyPrice)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.SellPrice)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.Stock)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.Description)
      </th>
      <th>
        @Html.DisplayNameFor(model => model.Remarks)
      </th>
      <th></th>
    </tr>

    @foreach (var item in Model) {
    <tr>
      <td>
        @Html.DisplayFor(modelItem => item.ProductId)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.ProductName)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.BuyPrice)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.SellPrice)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Stock)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Description)
      </td>
      <td>
        @Html.DisplayFor(modelItem => item.Remarks)
      </td>
      <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.ProductId }, new { @class = "btn btn-primary" }) | @Html.ActionLink("Delete", "Delete", new { id = item.ProductId }, new { @class = "btn btn-danger", onclick = "return confirm('Are sure wants to delete?');"
        })
      </td>
    </tr>
    }
  </table>

以下是我正在使用的模型:

public class Goods
{
  public int ProductId { get; set; }

  [DisplayName("Product Name")]
  public string ProductName { get; set; }

  public decimal BuyPrice { get; set; }
  public decimal SellPrice { get; set; }

  [Required]
  public int Stock { get; set; }

  public string Description { get; set; }
  public string Remarks { get; set; }
}

这是我的控制器

public List < Goods > GetGoods() 
{
  connection();
  List < Goods > goodslist = new List < Goods > ();

  SqlCommand cmd = new SqlCommand("GetAllGoods", con);
  cmd.CommandType = CommandType.StoredProcedure;

  SqlDataAdapter sd = new SqlDataAdapter(cmd);
  DataTable dt = new DataTable();

  con.Open();
  sd.Fill(dt);
  con.Close();

  foreach(DataRow dr in dt.Rows) {
    goodslist.Add(
      new Goods {
        ProductId = Convert.ToInt32(dr["ProductID"]),
          ProductName = Convert.ToString(dr["ProductName"]),
          BuyPrice = Convert.ToDecimal(dr["BuyPrice"]),
          SellPrice = Convert.ToDecimal(dr["SellPrice"]),
          Stock = Convert.ToInt32(dr["Stock"]),
          Description = Convert.ToString(dr["Description"]),
          Remarks = Convert.ToString(dr["Remarks"])
      });
  }

  return goodslist;
}

我的问题是,有没有可能在没有实体框架的情况下将这个数据表迁移到AJAX和jQuery?我一直在寻找,但问题解决了实体框架不使用SQL Server数据库.

预期输出应如此屏幕截图所示:

enter image description here

推荐答案

我想可以参考这一点

<script>  
        $(document).ready(function () {  
            $("#demoGrid").DataTable({  
  
                "processing": true, // for show progress bar  
                "serverSide": true, // for process server side  
                "filter": true, // this is for disable filter (search box)  
                "orderMulti": false, // for disable multiple column at once  
                "pageLength": 5,  
  
                "ajax": {  
                    "url": "/Demo/LoadData",  
                    "type": "POST",  
                    "datatype": "json"  
                },  
  
                "columnDefs":  
                [{  
                    "targets": [0],  
                    "visible": false,  
                    "searchable": false  
                },  
                {  
                    "targets": [7],  
                    "searchable": false,  
                    "orderable": false  
                },  
                {  
                    "targets": [8],  
                    "searchable": false,  
                    "orderable": false  
                },  
                {  
                    "targets": [9],  
                    "searchable": false,  
                    "orderable": false  
                }],  
  
                "columns": [  
                      { "data": "CustomerID", "name": "CustomerID", "autoWidth": true },  
                      { "data": "CompanyName", "name": "CompanyName", "autoWidth": true },  
                      { "data": "ContactName", "title": "ContactName", "name": "ContactName", "autoWidth": true },  
                      { "data": "ContactTitle", "name": "ContactTitle", "autoWidth": true },  
                      { "data": "City", "name": "City", "autoWidth": true },  
                      { "data": "PostalCode", "name": "PostalCode", "autoWidth": true },  
                      { "data": "Country", "name": "Country", "autoWidth": true },  
                      { "data": "Phone", "name": "Phone", "title": "Status", "autoWidth": true },  
                      {  
                          "render": function (data, type, full, meta)  
                          { return '<a class="btn btn-info" href="/Demo/Edit/' + full.CustomerID + '">Edit</a>'; }  
                      },  
                       {  
                           data: null, render: function (data, type, row) {  
                               return "<a href='#' class='btn btn-danger' onclick=DeleteData('" + row.CustomerID + "'); >Delete</a>";  
                           }  
                       },  
  
                ]  
  
            });  
        });  
    </script> 

来源:https://www.c-sharpcorner.com/article/using-datatables-grid-with-asp-net-mvc/

Jquery相关问答推荐

提交表单时运行自定义验证脚本

多个 AJAX 调用;获取所有失败的呼叫

使用带有 bootstrap 验证的 Ajax 函数时出现问题

jQuery在localStorage时将类添加到正文

如何使用 jQuery 强制悬停状态?

jQuery 绑定点击 *ANYTHING* 但 *ELEMENT*

在网络浏览器中,onblur 和 onfocusout 有什么区别?

JSLint 消息:未使用的变量

如何从 jQuery 中的父级中 Select 所有子级(任何级别)?

如何在 jQuery 中存储全局值(不一定是全局变量)?

iframe 仅显示页面的特定部分

jQuery .live() 和 .on() 有什么区别

jquery:更改URL地址而不重定向?

使用带有 HTML 表格的 jQuery UI 可排序

通过 AJAX 和 jQuery 使用 HTML5 文件上传

循环判断复选框并计算每个选中或未选中的复选框

将打开/关闭图标添加到 Twitter Bootstrap 可折叠项(手风琴)

查找 id 以开头的 html 元素

Twitter Bootstrap alert 消息关闭并再次打开

请求的资源错误中不存在Access-Control-Allow-Origin标头