AI OnAI Off
Any thoughts or ideas on what we may be doing wrong or could improve?
Thank you!
is it so that you do not receive model in action decorated with `[HttpPost]`? is that left blank intentionally?
The action that recieves the HttpPost is left blank intentionally because when filled in with the different models from my initial post (and below) the model state still comes back as valid or comes back with an error.
// ModelState comes back valid [HttpPost] [ValidateAntiForgeryToken] public ActionResult (ProfileBillingAddressEditPage model) { } // ModelState comes back valid [HttpPost] [ValidateAntiForgeryToken] public ActionResult (ProfileBillingAddressEditViewModel model) { } // Throws "no parameterless constructor" error [HttpPost] [ValidateAntiForgeryToken] public ActionResult (ProfileBillingAddressEditViewModel<ProfileBillingAddressEditPage> model) { }
Thank you,
Kevin Larsen
couple of cents:
what is specifically inside ModelState? which property is reported as error?
Hello,
We're in the process of moving over from Webforms to MVC and I'm currently working on two forms that are used by a customer to updated either their billing or their shipping address. I've gotten the form to the point that I'm able to update or create a new address without issue. However, the issue comes in when I'm trying to validate the fields based off of the ViewModel attributes for each of the fields. We've been using the Alloy PageViewModel and were having issues finding a way to create a separate ViewModel and still get the pages to load properly and ended up having to extend the page view for the form with the PageViewModel instead of the model of the page. The code that I have is below.
ProfileBillingAddressEditPage.cs (edited as fields are just for content areas on the page)
ProfileBillingAddressEditViewModel.cs (edited to keep short)
Index.cshtml (edited to keep short)
Controller (edited to keep short)
For pages that we don't create a view model for the ActionResult Index and Index.cshtml model call would look like below.
As can be seen we had to extend the PageViewModel and essentially duplicate it and then also include our additional data properties that we wanted to add. Trying to do this by just extending the view model with ProfileBillingAddressEditPage would result in either the view not being found (I believe because it went to the DefaultPageController also from the Alloy project), or would throw another error saying the models were incorrect. We don't like having to do it this way, but it was the only way we could figure out how to get it to work and wish there was a different way.
The issue with the validation on the server side is that when the form is submitted with invalid data or no data at all, in the [HttpPost] ActionResult Index the ModelState comes back as true for IsValid. I tried passing in the model multiple different ways as show below, with none of them working or the last one coming back with a "No parameterless constructor defined for this object" error.
Is the way that we are doing the view model (extending the PageViewModel) causing the ModelState issues? Is there a better way of doing the view model that we don't have to extend the PageViewModel (note I extended based off of the IPageViewModel with similar results, but didn't get to the point of testing the ModelState)?
Any help is greatly appreciated.
Thank you,
Kevin Larsen