【问题标题】:Show DataTable in MVC View在 MVC 视图中显示数据表
【发布时间】:2013-01-28 06:41:44
【问题描述】:

我想在 KnockoutJS 的帮助下将员工详细信息从 DataTable 绑定到 HTML 表中。这是我的模型:

public class Employee
{
    private string employeeCode;
    private string employeeName;

    public int ID { get; set; }

    [Required(ErrorMessage="Employee Code is Required")]
    public string EmployeeCode
    {
        get
        {
            return employeeCode;
        }
        set
        {
            employeeCode = value;
        }
    }

    [Required(ErrorMessage = "Employee Name is Required")]
    public string EmployeeName
    {
        get
        {
            return employeeName;
        }
        set
        {
            employeeName = value;
        }
    }
}

这是我使用 DataTable 的控制器代码。我将List<Employee> 传递给我的视图:

public JsonResult Get(int customerID)
{
    BAL.Employee dbProvider = new BAL.Employee();

    DataTable dataTable = dbProvider.ShowEmployeeDetails();

    List<Model.Employee> objExerciseList = new List<Model.Employee>();

    foreach (DataRow dataRow in dataTable.Rows)
    {
        Model.Employee objExercise = new Model.Employee();

        objExercise.ID = Convert.ToInt32(dataTable.Rows[0]["ID"].ToString());
        objExercise.EmployeeCode = dataTable.Rows[0]["EmpCode"].ToString();
        objExercise.EmployeeName = dataTable.Rows[0]["EmpName"].ToString();
        objExercise.ContactNumber = dataTable.Rows[0]["ContactNumber"].ToString();
        objExercise.MaritalStatus = Convert.ToBoolean(dataTable.Rows[0]["Is_MaritalStatus"].ToString());
        objExercise.EmailID = dataTable.Rows[0]["EmailID"].ToString();

        objExerciseList.Add(objExercise);
    }

    return Json(objExerciseList, JsonRequestBehavior.AllowGet);
}

最后是我的 View 和 ViewModel 页面和代码:

@model IEnumerable<Acidaes.CRMnext.TrainingExercises.Model.Employee>
@{
  ViewBag.Title = "exercise7";
  Layout = "../Shared/Master.cshtml";
 }
    <script src="../../Scripts/_references.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-1.6.2.js" type="text/javascript"></script>
    <script src="../../Scripts/knockout-2.2.1.js" type="text/javascript"></script>
    <script src="../../Scripts/json2.js" type="text/javascript"></script>
<html>
<head>
    <title>KO</title>
</head>
<body>
    <form action="" method="post">
        <div style="width: 990px; background-color: White; height: 710px;">
            <table id="tbllist" align="center" style="border: 5px #fff solid;">
                <tr>
                    <td colspan="6">
                        <h2>
                            Employee List</h2>
                    </td>
                </tr>
                <tr>
                    <td colspan="6" style="padding: 0px;">
                        <div id="title_p">
                            Listing</div>
                    </td>
                </tr>
                <tr>
                    <th align="left">
                        Employee Code
                    </th>
                    <th align="left">
                        Employee Name
                    </th>
                    <th align="left">
                        Contact Number
                    </th>
                    <th align="left">
                        Marital Status
                    </th>
                    <th align="left">
                        Email ID
                    </th>
                    <th align="left">
                    </th>
                </tr>
                <tbody>
                    <tr style="border-bottom: 1px solid #000000;">
                        <td>
                            @Html.LabelFor(model => model.EmployeeCode, new { data_bind = "text: EmpCode" })
                        </td>
                        <td>
                            @Html.LabelFor(model => model.EmployeeName, new { data_bind = "text: EmpName" })
                        </td>
                        <td>
                            @Html.LabelFor(model => model.ContactNumber, new { data_bind = "text: ContactNumber" })
                        </td>
                        <td>
                            @Html.CheckBoxFor(model => model.MaritalStatus, new { data_bind = "checked: MaritalStatus" })
                        </td>
                        <td>
                            @Html.LabelFor(model => model.EmailID, new { data_bind = "text: EmailID" })
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <script type="text/javascript">
         // Initialized the namespace
            var KnockoutDemoNamespace = {};

            // View model declaration
            KnockoutDemoNamespace.initViewModel = function (objExercise) {
                var customerViewModel = {
                    EmpCode: ko.observable(objExercise.EmployeeCode),
                    EmpName: ko.observable(objExercise.EmployeeName),
                    ContactNumber: ko.observable(objExercise.ContactNumber),
                    MaritalStatus: ko.observable(objExercise.MaritalStatus),
                    EmailID: ko.observable(objExercise.EmailID)
                };
                return customerViewModel;
            }

            // Bind the customer
            KnockoutDemoNamespace.bindData = function (objExercise) {
                // Create the view model
                var viewModel = KnockoutDemoNamespace.initViewModel(objExercise);

                ko.applyBindings(viewModel);
            }

            KnockoutDemoNamespace.getCustomer = function (customerID) {

                $.ajax({
                    url: "/Exercise/Get/",
                    type: 'post',
                    data: "{'customerID':'1' }",
                    contentType: 'application/json',
                    success: function (result) {
                        KnockoutDemoNamespace.bindData(result);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        var errorMessage = '';
                        $('#message').html(jqXHR.responseText);
                    }
                });
            }

            KnockoutDemoNamespace.addCustomer = function () {
                $.ajax({
                    url: "/Exercise/Add/",
                    type: 'post',
                    data: ko.toJSON(this),
                    contentType: 'application/json',
                    success: function (result) {

                        $('#message').html(result);
                    }
                });
            }

            $(document).ready(function () {
                KnockoutDemoNamespace.getCustomer(1);

            });
        </script>
    </form>
</body>
</html>

请帮助我,我对 KnockoutJS 完全陌生。如果我的问题有任何问题,请告诉我。

【问题讨论】:

  • 尝试分离服务器和客户端的问题。使用 JSON 和仅 html 视图创建 fiddle
  • 我没有太多的专业知识来创建一个小提琴..因为它只有 HTML 可以在其中放置我的 .cshtml 代码..bt 我应该将我的 Controller.cs 和属性定义在哪里Model.cs ..如果您希望我在 JSON 中进行相同的转换,那么我不知道..如果我在说一些愚蠢的事情..那么您可以预测我对 Knockout JS 的了解..所以在此基础上对待我.. .
  • 既然你问了......看看my edit to your question,这些都是你可以做的事情来改善你自己的问题(让别人帮助你变得更容易和更有趣)。此外,您提出的问题越多succinct,就越容易(也更有可能)提供帮助。最后,具体说明您的问题和tell us what you've tried 来解决问题(目前您只是将代码扔在那里,它读起来就像“请为我做这件事”的问题)。祝你好运!
  • @Jeroen 非常感谢你....没有人回答我的问题..

标签: model-view-controller knockout.js datatable


【解决方案1】:

KnockoutJS 旨在与 AJAX 和 JSON 一起使用。您的服务应以 JSON 格式返回数据。

也许这是一个好的开始,Making of JSON Webservice using C# .NET

【讨论】:

  • 但他返回的是 JSON? "返回 Json(objExerciseList, JsonRequestBehavior.AllowGet);"
  • 我怎样才能在我错的地方将数据返回为 JSON...因为我是全新的..我没有得到它..你能解释一下吗..因为我是在喂奶这个领域的宝贝..
  • @Richard :感谢您的帮助和回复...但链接也没有太大帮助...而且我没有制作任何类型的 WebService...它只是 MVC 4 和 Knockout Binding。 .to 显示一个 DataTable..
  • Knockout 是为客户端的 MVC 设计的。使用 MVC 4,所有代码都在服务器上。要么使用 RAZOR 和 MVC 4,要么使用带有返回 JSON 的 Web 服务的 Knockout。使用 KO,HTML 页面是静态的并且包含 KO 标记。请参阅@delixfe 提及的教程
  • 我在 MVC 4 和 Razor 中选择了一个项目......并且我希望在我的一个 .cshtml 页面上进行淘汰赛绑定以显示 DataTable......只是为了学习目的......跨度>
【解决方案2】:

您将表行绑定到员工对象列表,但您只绑定了实例。您可以为此使用淘汰赛的 foreach 绑定

        <tbody data-bind="foreach: model">
            <tr style="border-bottom: 1px solid #000000;">
                <td>
                    @Html.LabelFor(model => model.EmployeeCode, new { data_bind = "text: EmpCode" })
                </td>
                <td>
                    @Html.LabelFor(model => model.EmployeeName, new { data_bind = "text: EmpName" })
                </td>
                <td>
                    @Html.LabelFor(model => model.ContactNumber, new { data_bind = "text: ContactNumber" })
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.MaritalStatus, new { data_bind = "checked: MaritalStatus" })
                </td>
                <td>
                    @Html.LabelFor(model => model.EmailID, new { data_bind = "text: EmailID" })
                </td>
            </tr>
        </tbody>

你知道淘汰赛的教程吗?掌握这些概念真的很棒:http://learn.knockoutjs.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多