码迷,mamicode.com
首页 > Web开发 > 详细

MVC4实现批量更新数据

时间:2015-04-11 00:00:53      阅读:2136      评论:0      收藏:0      [点我收藏+]

标签:

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();
            }
        }

运行效果:

技术分享

MVC4实现批量更新数据

标签:

原文地址:http://www.cnblogs.com/sky-net/p/4415956.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!