将pagedlist从视图发布到控制器时出错

时间:2017-05-12 06:13:26

标签: asp.net-mvc razor

我在MVC中有一个索引页面,其中的数据以数据库的表格格式显示并使用分页。我只想编辑表格的一列。我可以将分页列表发布到控制器进行编辑。如果是这样的话? 我可以在传递PagedList

时使用普通列表进行编辑获取错误

---这是我的索引页----

@model PagedList.IPagedList<emp_prac.Models.Employee>
@using PagedList.Mvc;

@*@model List<emp_prac.Models.Employee>*@

@using (Html.BeginForm("UpdateOrder", "Employee", FormMethod.Post))
{
    Html.AntiForgeryToken();
    Html.EditorForModel();
    ViewBag.Title = "Employee Details";

<h2>Employee Details</h2>

<p>
    @Html.ActionLink("Create New", "InsertEmployee")
</p>

<table class="table">
    <thead>
        <tr>
            <th>Edit</th>
            <th>Delete</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone No</th>
            <th>Salary</th>
            <th>Joining Date</th>
            <th>PDF</th>
            <th>Status</th>
            <th>Order</th>
        </tr>
    </thead>
    <tbody>
@for (int i = 0; i < Model.Count; i++)
{
    <tr>
        <td>@Html.ActionLink("Edit", "EditEmployee", new { id = Model[i].emp_id }) </td>
        <td>@Html.ActionLink("Delete", "DeleteEmployee", new { id = Model[i].emp_id }, new { onclick = "return confirm('Are you sure you want to delete?');", @class = "btn-btn-default" }) </td>
        <td>@Html.DisplayFor(model => model[i].emp_name)</td>
        <td>@Html.DisplayFor(model => model[i].emp_email)</td>
        <td>@Html.DisplayFor(model => model[i].emp_phone_no)</td>
        <td>@Html.DisplayFor(model => model[i].emp_salary)</td>
        <td>@Html.DisplayFor(model => model[i].emp_joining_date)</td>
        <td>@Html.ActionLink("PDF", "UploadPdf", new { id = Model[i].emp_id }) </td>
        <td>@(Html.DisplayFor(model => model[i].emp_status).ToString() == "1" ? "Active" : "Inactive")</td>
        <td>@Html.TextBoxFor(model => model[i].emp_order, new { style = "width: 35px" })</td>
        <td>@Html.HiddenFor(model => model[i].emp_id)</td>
    </tr>
}
        </tbody>
</table>
<button type="submit" value="Order" onclick="location.href='@Url.Action("UpdateOrder","Employee")'">Order</button>
}
    <br />
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))`

---这是我的控制器动作方法---

 [HttpPost]
        public ActionResult UpdateOrder(PagedList<Employee> obj_view)
        {
            foreach (var abc in obj_view)
            {
                Employee obj = new Employee();
                obj.emp_order = abc.emp_order;
                obj.emp_id = abc.emp_id;
                obj.Mode = "O";
                Employee.InsertUpdateEmployee(obj);
            }
            return RedirectToAction("Index");
        }

1 个答案:

答案 0 :(得分:0)

我深入研究了您的代码,根据我的观点,下面的行可能会导致错误。

**<button type="submit" value="Order" onclick="location.href='@Url.Action("UpdateOrder","Employee")'">Order</button>
}**

<强>原因:

当您通过表单发布数据时,无需在提交按钮中指定onclick

<强>解决方案:

只需用这样的简单提交按钮替换上面的行

**<input type="submit" value="Order" class="btn btn-default" />**

在按钮单击事件中查看是否使用断点获取数据。

希望您现在可以获得数据,请让我知道您的想法或反馈

由于 KARTHIK