Skip to content

Instantly share code, notes, and snippets.

@rockfordlhotka
Last active January 23, 2023 09:54
Show Gist options
  • Save rockfordlhotka/0474af9cf53cec9f2d359fa413d61e73 to your computer and use it in GitHub Desktop.
Save rockfordlhotka/0474af9cf53cec9f2d359fa413d61e73 to your computer and use it in GitHub Desktop.
Simple edit list of items in ASP.NET MVC
@* Goes in Views\PersonList *@
@using EditableListMvc.Models
@model IEnumerable<Person>
@{
ViewBag.Title = "Person List";
}
<h2>Person List</h2>
@using (Html.BeginForm("Index", "PersonList", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Remove)
</th>
</tr>
</thead>
<tbody id="childtable">
@Html.EditorFor(model => Model)
</tbody>
</table>
<p>
<input name="command" type="submit" value="Add Item" />
<input name="command" type="submit" value="Remove Selected" />
<input name="command" type="submit" value="Cancel/Refresh" />
</p>
<p>
@Html.ValidationSummary()
<input name="command" type="submit" value="Submit" />
</p>
}
// Goes in Models
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EditableListMvc.Models
{
public class Person
{
public int PrimaryKey { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public bool Remove { get; set; }
}
}
@* Goes in Views\PersonList\EditorTemplates *@
@using EditableListMvc.Models
@model Person
<tr>
<td>
@Html.HiddenFor(model => model.PrimaryKey)
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</td>
<td>
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</td>
<td>
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</td>
<td>
@Html.EditorFor(model => model.Remove)
</td>
</tr>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace EditableListMvc.Controllers
{
public class PersonListController : Controller
{
// TODO: remove mock database
private static List<Models.Person> MockDb = new List<Models.Person>
{
new Models.Person { PrimaryKey = 1, FirstName = "Fred", LastName="Than", Age=54 },
new Models.Person { PrimaryKey = 2, FirstName = "Erin", LastName="Saavedra", Age=37 },
new Models.Person { PrimaryKey = 4, FirstName = "Abdul", LastName = "Banas", Age = 12 }
};
private List<Models.Person> _personList;
private List<Models.Person> PersonList
{
get
{
if (_personList == null)
_personList = Session["PersonList"] as List<Models.Person>;
return _personList;
}
set
{
_personList = value;
Session["PersonList"] = _personList;
}
}
// GET: PersonList
public ActionResult Index()
{
if (PersonList == null)
{
// TODO: load real data from database
PersonList = MockDb;
}
return View(PersonList);
}
[HttpPost]
public ActionResult Index(List<Models.Person> personList, string command)
{
try
{
if (command == "Add Item")
{
personList.Add(new Models.Person { PrimaryKey = -1 });
PersonList = personList;
}
else if (command == "Remove Selected")
{
int pos = personList.Count();
while (pos > 0)
{
pos--;
if (personList[pos].Remove)
personList.RemoveAt(pos);
}
PersonList = personList;
}
else if (command == "Cancel/Refresh")
{
// force reload of data from database
PersonList = null;
}
else
{
// update actual database
MockDb = personList;
// force reload of data from database
PersonList = null;
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
@AdriaanJvR
Copy link

I wish all examples on the web were as clear and concise as this one!

@VladimirtheGreatest
Copy link

You are genius.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment