1

I am using asp.net core mvc 5 with EF Core 5.

I have a page, that when a button is pushed a modal form pops up. The user then can create a new person. When the user then presses submit on the modal form the person is created and linked to a centre. I want the parent page to be refreshed so the it shows the new person. The code below is what I have at the moment.

The button on the main page is

<button type="button" class="close ml-3 mb-3"
   data-toggle="ajax-modal" data-target="#personModal"
   data-url="@Url.Action("Create", new { id = Model.Id })"
   title="Click to change Person Responsible">
   +
</button>

I use the following javascript to open the modal -

$(function () {
    var placeHolderElement = $('#PlaceHolderHere');
    $('button[data-toggle="ajax-modal"]').click(function (event) {
        var url = $(this).data('url');

        $.get(url).done(function(data) {
            placeHolderElement.html(data);
            placeHolderElement.find('.modal').modal('show');
        })
    })
})

I use the following to submit the modal form -

<input type="submit" value="Submit" name="Submit" class="btn btn-success float-right" id="SubmitForm" />

In my controller -

[HttpGet]
public IActionResult Create(int id)
{
   List<Title> titleList = _titleRepository.Titles.ToList();
   ViewBag.TitleList = titleList;

   var personListViewModel = new PersonListViewModel
   {
      persons = _personRepository.Persons,
      person = new Person(),
      vaccinationCentreId = id
   };

   return PartialView("_PersonModalPartial", personListViewModel);
}

[HttpPost]
public ActionResult Create(PersonListViewModel pers)
{         
   _personRepository.CreatePerson(pers.person);
   Person newPers = _personRepository.GetPersonByName(pers.person.Forename, pers.person.Surname);

   VaccinationCentre vaccCentre = _vaccCentreRepository.GetVaccinationById(pers.vaccinationCentreId);

   vaccCentre.PersonResponsableId = newPers.Id;
   _vaccCentreRepository.UpdateVaccinationCentre(vaccCentre);

   return View("Details", vaccCentre.Id);
}

My Details code -

public IActionResult Details(int id)
{
   var vaccCentre = _vaccCentreRepository.GetVaccinationById(id);

   if (vaccCentre == null)
   {
      return NotFound();
   }

   List<Title> titleList = _titleRepository.Titles.ToList();
   ViewBag.TitleList = titleList;

   List<HealthBoard> healthBoardList = _healthBoardRepository.HealthBoards.ToList();
   ViewBag.HealthBoardList = healthBoardList;

   List<CentreType> centreTypeList = _centreTypeRepository.CentreTypes.ToList();
   ViewBag.CentreTypeList = centreTypeList;

   List<VaccinationAvailableTo> vaccAvailableToList = _availableToRepository.VaccinationAvailableTos.ToList();
   ViewBag.VaccAvailableToList = vaccAvailableToList;

   List<Person> personList = _personRepository.Persons.ToList();
   ViewBag.PersonList = personList;

   return View(vaccCentre);
}

[HttpPost]
public ActionResult Details(VaccinationCentre vaccCentreModel)
{
   if (!ModelState.IsValid)
   {
      return View(vaccCentreModel);
   }

   _vaccCentreRepository.UpdateVaccinationCentre(vaccCentreModel);
            
   return RedirectToAction("Index", "Admin");
}

What I am trying to achieve is when the Create Post is done that the Details(int Id) is run to refresh the page. But It is trying to run Details(VaccinationCentre vaccCentreModel).

I basically want the parent pge to refresh with the new data.

Can anyone help?

Kev
  • 109
  • 1
  • 11

1 Answers1

1

I figured it out -

I used -

 $.get("/VaccinationCentre/Details/" + $("#vaccCentreId").val()).done(function (data) {
            placeHolderElement.find('.modal').modal('hide')
            location.reload();
 })

Kev
  • 109
  • 1
  • 11