【发布时间】:2021-07-15 10:30:31
【问题描述】:
我正在使用 asp.net core mvc 5 和 EF Core 5。
我有一个页面,当按下按钮时会弹出一个模态表单。然后用户可以创建一个新人。当用户然后在模态表单上按下提交时,该人被创建并链接到中心。我希望刷新父页面,以便显示新人。下面的代码是我目前所拥有的。
主页上的按钮是
<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>
我使用以下 javascript 打开模态 -
$(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');
})
})
})
我使用以下提交模态表单-
<input type="submit" value="Submit" name="Submit" class="btn btn-success float-right" id="SubmitForm" />
在我的控制器中 -
[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);
}
我的详细信息代码 -
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");
}
我想要实现的是,当 Create Post 完成时,运行 Details(int Id) 以刷新页面。但它正在尝试运行 Details(VaccinationCentre vccCentreModel)。
我基本上希望父页面用新数据刷新。
谁能帮忙?
【问题讨论】:
-
您可以尝试在stackoverflow.com/a/30303312/2943218 此处使用此答案,然后在其中重新加载页面
-
你能告诉我你现在使用的是哪个引导版本吗?
标签: javascript jquery asp.net-core model-view-controller bootstrap-4