zoukankan      html  css  js  c++  java
  • MVC4实现批量更新数据

    Html:

    @using (Html.BeginForm("Edit", "Home"))
    {
        <div>
            <input type="submit" value="保存" class="delete" />
        </div>
        <table>
            <tr>
                <th>@Html.CheckBox("chackall")</th>
                <th>名称</th>
                <th>排序号</th>
                <th>操作</th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td><input name="id" value="@item.id" type="hidden" /></td>
                    <td><input name="name" value="@item.Name" type="text" /></td>
                    <td><input name="sortid" value="@item.SortId" type="text" /></td>
                    <td>@Html.ActionLink("详细", "Details", new { id = "1" })</td>
                </tr>
            }
        </table>
    }

    Action:

            [HttpPost]
            public ActionResult Edit(List<int> id, List<string> name, List<int> sortid)
            {
                ApplicationContext db = new ApplicationContext();
                try
                {
                    for (int i = 0; i < id.Count(); i++)
                    {
                        var K = db.Categories.Where(m => m.id == id[i]).First();
                        K.Name = name[i];
                        K.SortId = sortid[i];
                    }
                    db.SaveChanges();
                   
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }

    运行效果:

  • 相关阅读:
    POJ 1680 Fork() Makes Trouble
    课堂改进意见
    梦断代码 读后感3
    梦断代码 读后感2
    找一问题
    软件评价——搜狗输入法
    《梦断代码》读后感1
    站立会议第十天
    站立会议第九天
    站立会议第八天
  • 原文地址:https://www.cnblogs.com/sky-net/p/4415956.html
Copyright © 2011-2022 走看看