简体   繁体   中英

Something Like Win App DataGridView in ASP.NET MVC3 With Update All Elements

我在ASP.NET MVC3和每行前面的索引视图中都有一个简单的模型,出现3个链接(“编辑”,“详细信息”,“删除”)。使用“编辑链接”,页面导航到编辑视图,您可以编辑该特定行,所以我很有趣在一起编辑所有行,就像通过DataGridView在Win应用程序中一样,例如在“索引”视图中的表是“可编辑的”和“全部更新”按钮,保存所有行的编辑,是否有人对此有任何想法?

you can edit index view, like the following:

1- Replace @Html.DisplayFor with @Html.EditorFor for that columns you want to edit,

2- Add @using (Html.BeginForm()){} to included all of <table> tag,

3- Add a submit like this: <input type="submit" value="Updata All" /> inside of BeginForm() block

4- Add a new [HttpPost] for Index action in your controller and handle edits you can use something like this:

[HttpPost]
    public ActionResult Index(FormCollection collection)
    {
        string[] Descriptions = collection.GetValues("item.Description");

       for (int i = 1; i <= Descriptions.Length; i++)
        {
            MyModel element = db.MyModels.Find(i);
            element.Description = Descriptions[i - 1];
            db.Entry(element).State = EntityState.Modified;
            db.SaveChanges();
        }

        return View(db.MyModels.ToList());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM