Mam problem z dodawaniem do listy elementu. Wszystko jest robione w mvc. W widoku index wyświetlam wszystkie elementy tej listy potem przechodzę do tworzenia nowego elementu akcja kontrolera create , tam się dodaje ale gdy wraca do wyświetlenia elementów listy (index) to lista pozostaje bez zmian.Próbowałem zmieniać sposób tworzenia listy, zmiany listy na inną formę przechowywania jednak zawsze było tak samo.
kod klasy PersonRepository pełniąca funkcje modelu w mvc
public class PersonRepository : IPersonRepository
{
IList<Person> data = new List<Person>(){ new Person { FirstName = "imie1", SecondName = "nazwisko1", Age = 20,BirthDay=new DateTime(1999, 6, 1), Addres="adres",Code="kod1" },
new Person { FirstName = "imie2", SecondName = "nazwisko2", Age = 21, BirthDay=new DateTime(1999, 6, 1),Addres="adres",Code="ko2d"},
new Person { FirstName = "imie3", SecondName = "nazwisko3", Age = 22, BirthDay=new DateTime(1999, 6, 1),Addres="adres",Code="kod3"}};
public IList<Person> GetDbData()
{
return data;
}
public IList<Person> AddDbData(Person o)
{
//Person newPerson = new Person();
//newPerson.Code = o.Code;
//newPerson.Addres = o.Addres;
//newPerson.Age = o.Age;
//newPerson.BirthDay = o.BirthDay;
//newPerson.FirstName = o.FirstName;
//newPerson.SecondName = o.SecondName;
data.Add(o);
return data;
}
}
}
kod kontrolera person
public class PersonController : Controller
{
// GET: Person
private PersonService dataService;
public PersonController()
{
this.dataService = new PersonService(new PersonRepository());
}
// GET: Person
public ActionResult Index()
{
var model = new List<Person>(dataService.GetData());
return View(model);
}
// POST: Person/Create
public ActionResult Create()
{
return View();
}
//POST: Person/Create
//To protect from overposting attacks, please enable the specific properties you want to bind to, for
//more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Person newPerson)
{
var model = new List<Person>(dataService.GetData());
if (ModelState.IsValid)
{
model.Add(newPerson);
model =new List<Person>(dataService.AddData(newPerson));
return RedirectToAction("Index");
}
else
{
return View(newPerson);
}
}