I have a slider that has a registration form, I create the form as a partial View . this is the method of create registration form: Get method:
public IActionResult CorporationRegisterRequest()
{
CorporationRequestViewModel corporationViewModel = new CorporationRequestViewModel();
return PartialView("CorporationRegisterPartial", corporationViewModel);
}
Post Method
[HttpPost()]
[Route("CorporationRegister")]
public async Task<IActionResult> CorporationRegisterRequest(CorporationRequestViewModel corporationRequestViewModel)
{
if (ModelState.IsValid)
{
var result = await _userService.CreateCorporationRequest(corporationRequestViewModel);
switch (result)
{
case CorporationRequestResult.Failed:
TempData[ErrorMessage] = "NotFound";
break;
case CorporationRequestResult.UserHasNotRequestWithThisNumber:
TempData[WarningMessage] = "User Has no Request with this Number";
break;
case CorporationRequestResult.Successfully:
TempData[SuccessMessage] = "Your Request send, Our Staff Calling You As soon As Possible";
return RedirectToAction("Index", "Home");
}
}
return return PartialView("CorporationRegisterPartial", corporationRequestViewModel);
'
If model was valid , every thing is ok and add to database. But if Model state is not valid, it had to return to index page , index includes that partial how can I return to partial ? I have the problem in last return in my method, that shows "corporationRequestViewModel" and index at same time if not valid model
@Mikhail I use this code first , but when is not valid , it returns just view of partial view , without any style of that page and without index, I want to return to home page and show the partial view, I update the question and add the picture of return partial
In general, you can return it using the PartialView result (like you do it in the above GET method) + pass the bound Model instance (from action method argument) as a parameter (to keep / visualize model state errors):
public async Task<IActionResult> CorporationRegisterRequest(CorporationRequestViewModel corporationRequestViewModel) {
if (ModelState.IsValid) {
...
}
//return RedirectToAction("CorporationRegisterRequest", PartialView("CorporationRegisterPartial", corporationRequestViewModel));
return PartialView("CorporationRegisterPartial", corporationRequestViewModel);
}
However, it seems to be valid if you do an async POST request. Otherwise, you may need to return the entire View result and transfer model Model instance instead of creating a new instance, for example, using a TempData.