开发者社区> 问答> 正文

加载视图与之前的现有数据进行编辑记录在运行update存储过程之前?

我有下面的控制器操作方法。

public int BulkUpdate(List employees) {

       Connection();
       employees = GetAllEmployees().ToList();
        // using reflection, i mean why did i use this ??
        //used it for converting the generic list into datatables.
       DataTable dt = new DataTable(typeof(Employee).Name);
       PropertyInfo[] props = typeof(Employee).GetProperties(BindingFlags.Public | BindingFlags.Instance);
       foreach (var prop in props)
       {
           dt.Columns.Add(prop.Name);
       }

       foreach (var employee in employees)
       {
           var values = new object[props.Length];
           for (int i = 0; i < props.Length; i++)
           {
               values[i] = props[i].GetValue(employee, null);
           }

           dt.Rows.Add(values);
       }
       //
       using (SqlCommand cmd = new SqlCommand("BulkUpdateEmployee", con))
       {
           cmd.CommandType = CommandType.StoredProcedure;
           SqlParameter parameter = cmd.Parameters.AddWithValue("@tblEmployeeType", dt);
           parameter.SqlDbType = SqlDbType.Structured;
           parameter.TypeName = "dbo.EmployeeType";


           con.Open();
           var rowsAffected = cmd.ExecuteNonQuery();
           con.Close();

           return rowsAffected;
       }//using ends here

   }

and this method is being called in main controller:

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] public JsonResult BulkUpdateOrInsert(List employees) { int rowsAffected=empRepo.BulkUpdate(employees); return Json(rowsAffected, JsonRequestBehavior.AllowGet);

   }

这是视图文件:

@using System.Collections
@model IEnumerable<Employee_Management_System.Models.Employee>

@{
    ViewBag.Title = "BulkUpdateOrInsert";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script type="text/javascript">
    $("body").on("click", "#btnSave", function() {
            //Have to Loop through the Table rows and build a JSON array maybe try passing it to Controller action.
        var employees = new Array();
            debugger;
            $("#tblEmployees tbody tr").each(function() {
                var row = $(this);
                var employee = {};
            employee.Name = row.find("td").eq(0).html();
            employee.City = row.find("td").eq(1).html();
            employee.Department = row.find("td").eq(2).html();
            employee.Gender = row.find("td").eq(3).html();
            employees.push(employee);
        });
    </script>
    @*end of section script*@

    <script type="text/javascript">
                $.ajax({
                    type: "POST",
                url: "/Employee/BulkUpdateOrInsert",
                data: JSON.stringify(employees),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                    success: function(r) {
                    alert(r + " record(s) inserted.");
            }
});
    </script>
</head>
<body>
    <h2>BulkUpdateOrInsert</h2>


    @using (Html.BeginForm("BulkUpdateOrInsert", "Employee", "POST"))
    {
        @Html.AntiForgeryToken()


        <h4>Employee</h4>
        <hr />

        <table class="table" id="tblEmployees">
            <thead>
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.Name)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.City)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Department)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Gender)
                    </th>
                    <th></th>
                </tr>
            </thead>

            @foreach (var item in Model)
            {
                <tbody>
                <tr>
                    <td>@Html.HiddenFor(modelItem => item.EmployeeId)</td>
                    <td>
                        @Html.EditorFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.City)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.Department)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.Gender)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new {id = item.EmployeeId}) |
                        @Html.ActionLink("Details", "Details", new {id = item.EmployeeId}) |
                        @Html.ActionLink("Delete", "Delete", new {id = item.EmployeeId})
                    </td>
                </tr>
                </tbody>
            }

        </table>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" id="btnSave" value="Save All" class="btn btn-default" />
            </div>
        </div>
        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
        @section Scripts {
            @Scripts.Render("~/bundles/jqueryval")
        }
    }
</body>

</html>

问题是,视图不加载更新记录。 我的要求是负载与SQL Server数据库中已有的数据视图。 编辑记录使用HTML.EditFor返回通用的列表更新记录- - >将其转换成数据表,并通过数据表存储过程。 (本我)。 问题是我不能加载视图更新数据库中的条目。 直接存储过程正在加载视图之前执行。

存储过程:

USE EMPLOYEE_Db GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO

CREATE PROCEDURE [dbo].[BulkUpdateEmployee] @tblEmployeeType [dbo].[EmployeeType] READONLY AS BEGIN SET NOCOUNT ON; UPDATE dbo.EMPLOYEE SET Employee.Name=et.Name, Employee.City=et.City, Employee.Department=et.Department, Employee.Gender=et.Gender

FROM dbo.EMPLOYEE INNER JOIN @tblEmployeeType AS et ON dbo.EMPLOYEE.EmployeeId=et.EmployeeId

END

展开
收起
SONGYiiiD 2019-12-06 21:43:02 1563 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载