ASP.Net MVC - ModelState.AddModelError when GET / POST have different models

I have a use case where I used different models for GET and POST actions in my controller. This works great for my view because most of the data ends up in labels. The GET method model contains 10 properties, but the POST model only needs 3.

This GET view renders a form that only requires 3 of these properties, not all 10. So the POST method model takes a model class that only contains these 3 properties. So the ASP.Net MVC model binding property populates the model class parameter in my POST method with only these 3 required properties and you are good.

Here the question . When I run into a business rule violation in the POST method and want to use ModelState.AddModelError and re-render the original view, I no longer have 7 properties that weren't POSTED since they were not part of the form and are not part of the model class which this method takes as a parameter.

I am currently calling the constructor to return an instance of the model class for the POST method, and the GET method itself delegates to the same builder. So in these cases, when there is a business rule violation in the POST method, I return a View ("OriginalGetView", originalGetModel). How can I use ModelState.AddModelError in this case in the POST method if I want to send custom messages back to the view using a completely different model class?

It was too lazy to use the same model class for the GET and POST methods, given that their needs were so different. What's the best practice here? I see a lot of people recommending using the same model for both methods, and POST all fields back from hidden form fields, but that just seems like a waste in most cases, and it seems ugly to be sending things like "VendorName". back to the server when I already have a "VendorId".

+3


source to share


3 answers


I may not understand what you are trying to do, but make sure you are not penny and wise. I see that you can just post IDs, not necessarily descriptors. But it looks like you should re-render the view after posting ... if you can just access the properties of the model if you post the same model as in get. If you only publish IDs, you have to waste time re-accessing the database to get the description values ​​(like vendorname as you described) using the vendor ID no? Wouldn't that be additional processing? As I said, I might misunderstand your post, but for consistency, using the same view model as your opinion and post makes sense to me.



+1


source


Hidden inputs is probably the best solution here, I think even on 2g you shouldn't create any lag, unless the values ​​of your model properties are long strings or something encrypted or xml.

So your .cshtml will have this in it for 4 properties that are not currently included in the form:

<form>
    @Html.HiddenFor(m => m.Property1)
    @Html.HiddenFor(m => m.Property2)
    @Html.HiddenFor(m => m.Property3)
    @Html.HiddenFor(m => m.Property4)

      



But you can also get model state errors from the original published model and recreate the ModelError state in your response model to get around the use of hidden inputs.

I just found this tutorial (not an answer using Green Checkmark, but the highest level answer: ASP.NET MVC - How to store ModelState errors through RedirectToAction?

Note. If you need to copy model properties from a model to another model (of the same type or a different type) in a cleaner way, choose AutoMapper.

+1


source


Perhaps this could help with what you were trying to achieve - model-level errors that would not need to be bound to a specific field / property, but can be mapped to the global scope.

fooobar.com/questions/145197 / ...

0


source







All Articles