public IActionResult Index(PersonModel personModel) { List <PersonModel> people = new List <PersonModel>(); JSONReadWrite readWrite = new JSONReadWrite(); people = JsonConvert.DeserializeObject <List <PersonModel> >(readWrite.Read("people.json", "data")); PersonModel person = people.FirstOrDefault(x => x.Id == personModel.Id); if (person == null) { people.Add(personModel); } else { int index = people.FindIndex(x => x.Id == personModel.Id); people[index] = personModel; } string jSONString = JsonConvert.SerializeObject(people); readWrite.Write("people.json", "data", jSONString); return(View(people)); }
public IActionResult Index() { List <PersonModel> people = new List <PersonModel>(); JSONReadWrite readWrite = new JSONReadWrite(); people = JsonConvert.DeserializeObject <List <PersonModel> >(readWrite.Read("people.json", "data")); return(View(people)); }
public IActionResult Delete(int id) { List <PersonModel> people = new List <PersonModel>(); JSONReadWrite readWrite = new JSONReadWrite(); people = JsonConvert.DeserializeObject <List <PersonModel> >(readWrite.Read("people.json", "data")); int index = people.FindIndex(x => x.Id == id); people.RemoveAt(index); string jSONString = JsonConvert.SerializeObject(people); readWrite.Write("people.json", "data", jSONString); return(RedirectToAction("index", "Person")); }