Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
}
}
}
}
@steveculshaw

This comment has been minimized.

Copy link

steveculshaw commented Jun 8, 2016

Nice ... thanks for sharing

@drmcclelland

This comment has been minimized.

Copy link

drmcclelland commented Jun 8, 2016

Short, simple, to-the-point! It's refreshing to see an example that doesn't require "large framework XYZ" or tons of external libraries. Thanks!

@Bharatkumarsharma49

This comment has been minimized.

Copy link

Bharatkumarsharma49 commented Feb 4, 2017

the code made briefly ..its to large ..

@Oracularman

This comment has been minimized.

Copy link

Oracularman commented Mar 15, 2018

How do you implement session in .NET Core ???

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddSessionStateTempDataProvider();

services.AddSession();

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
app.UseMvcWithDefaultRoute();
}

@SimoneLinder

This comment has been minimized.

Copy link

SimoneLinder commented Jun 16, 2018

Great help, but really struggling to link to my data
I get the following shown repeatedly rather than list of data
System.Collections.Generic.HashSet`1[KGMComplaints.Models.ctb_Complainant]
Any ideas please

@AdamHeff

This comment has been minimized.

Copy link

AdamHeff commented Sep 28, 2018

A very useful example. Thank you.

@EvangelineKumar

This comment has been minimized.

Copy link

EvangelineKumar commented Oct 17, 2018

Very useful, thanks for sharing.

@richboud

This comment has been minimized.

Copy link

richboud commented Dec 19, 2019

Genius! I love it.

@Marceloromeugoncalves

This comment has been minimized.

Copy link

Marceloromeugoncalves commented Jan 13, 2020

Very useful example. Simple and objective. Thank you.

@AdriaanJvR

This comment has been minimized.

Copy link

AdriaanJvR commented Mar 24, 2020

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

@VladimirtheGreatest

This comment has been minimized.

Copy link

VladimirtheGreatest commented Jun 10, 2020

You are genius.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.