Last active
January 23, 2023 09:54
-
-
Save rockfordlhotka/0474af9cf53cec9f2d359fa413d61e73 to your computer and use it in GitHub Desktop.
Simple edit list of items in ASP.NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@* 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> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@* 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
Very useful, thanks for sharing.
Genius! I love it.
Very useful example. Simple and objective. Thank you.
I wish all examples on the web were as clear and concise as this one!
You are genius.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A very useful example. Thank you.