Пример #1
0
        public ActionResult Create(PersonModel personModel)
        {
            List <PersonModel> people    = new List <PersonModel>();
            JSONReadWrite      readWrite = new JSONReadWrite();

            people = JsonConvert.DeserializeObject <List <PersonModel> >(readWrite.Read("Person.json"));

            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("Person.json", jSONString);

            return(RedirectToAction("Index", "Home"));
        }
Пример #2
0
        public ActionResult Delete(int id)
        {
            List <PersonModel> people    = new List <PersonModel>();
            JSONReadWrite      readWrite = new JSONReadWrite();

            people = JsonConvert.DeserializeObject <List <PersonModel> >(readWrite.Read("Person.json"));

            int index = people.FindIndex(x => x.Id == id);

            people.RemoveAt(index);

            string jSONString = JsonConvert.SerializeObject(people);

            readWrite.Write("Person.json", jSONString);

            return(RedirectToAction("Index", "Home"));
        }