-
-
Save rockfordlhotka/0474af9cf53cec9f2d359fa413d61e73 to your computer and use it in GitHub Desktop.
@* 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(); | |
} | |
} | |
} | |
} |
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!
the code made briefly ..its to large ..
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();
}
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
A very useful example. Thank you.
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.
Nice ... thanks for sharing