1

I currently have a view component which contains a form. Once this form is submitted it is sent to the controller and the model state is checked. Normally after the model state is not valid I just reload the view with the model passed in and display the values in the with the validation error messages. My issue is because I'm using a view component how do I get this model with the validation error messages back to the view component from the controller. I need to get them model through the view and to the view component.

Say that I submit the form with data that would fail model validation and the controller method below is hit, after the model state is checked and fails, how would I return it back to the view component?

public async Task<IActionResult> UpdateDetails(CustomerDetailsViewModel customerDetailsViewModel)
{
    if (ModelState.IsValid)
    {
         ...
    }
    //How to return 'customerDetailsViewModel' back to view component
}
KB_Shayan
  • 632
  • 8
  • 24
  • I think you might be looking for something along the lines of this: https://stackoverflow.com/questions/5212248/get-error-message-if-modelstate-isvalid-fails – devklick Feb 23 '20 at 22:39
  • No that is not what I want. That works when I have just a regular view. I already have all the validation set up on the view component. I just need a way of getting the model to the view component from the controller. @Klicker – KB_Shayan Feb 23 '20 at 22:51
  • Doesn't your view component sit on a view though (where your form is)? So you'll still actually return your view, then pass the model to your view component which should have it as an parameter. – Ben Sampica Feb 24 '20 at 13:27

1 Answers1

-1

All you need is to return the view of the model following the snippet below:

if(ModelState.IsValid)
{
  //What you want to do

}
else
{
return View(customerDetailsViewModel);
}

Returning the view of customerDetailsViewModel will return all the data previously filled by the customer