Why, when transferring the model to my .Net MVC 4 Controller Action, does she insist on using it in a partial update?

I intend to pass the Hotel model into my action with the controller - do some checks / processing on it, and then return a potentially different Hotel model presented in a partial view,

The problem I get is that if I pass Model oHotelParameter to Action, then PartialView uses the model passed to Action instead of the one passed to the PartialView method.

If I remove the oHotelParameter parameter from the action, then the view is displayed as expected using oHotel .

public ActionResult _SaveMasterDetails(Hotel oHotelParameter) { //Do some processing on oHotelParameter //........ Hotel oHotel = new Hotel(); oHotel.GetHotelInfoById(14); //This gets a different Hotel object just for a test //For some reason oHotel is ignored and oHotelParameter is used instead unless I remove oHotelParameter return PartialView("_MasterDetails", oHotel); } 

When I debug the view, I see that the model is set to the value that I pass to PartialView ( oHotel ), but the result that I see in the return from Action contains data from oHotelParameter .

If that matters, I call the action from jQuery ajax.

Can someone explain why this should happen?

+4
source share
2 answers

when mvc processes the form message, it populates the ModelState with model details.

This is when used, when the view is displayed again from the post action, it means that you dropped the view because it failed the validation.

If you want to transfer the new model and not use the state of the view, you can call ModelState.Clear () before returning the view, and this should allow you to reconnect the view to the new model.

+3
source

I think this would help if you had a better understanding of how model binding works when you send back an action method. In most cases, it is not necessary and inefficient to pass the view model as a parameter to the POST action method. What you are doing is loading the view model twice into memory when you pass the view model as a parameter (assuming a strongly typed view). When you send a message back, the model becomes part of the collection of forms (by binding to the model) in the request object in the BaseController class that each controller inherits. All you have to do is retrieve the model from the Form collection in the Request object in BaseController. It so happened that there is a convenient TryUpdateModel method that will help you with this. This is how you do it.

 [POST] public ActionResult Save() { var saveVm = new SaveViewModel(); // TryUpdateModel automatically populates your ViewModel! // TryUpdateModel also calls ModelState.IsValid and returns // true if your model is valid (validation attributes etc.) if (TryUpdateModel(saveVm) { // do some work int id = 1; var anotherSaveVm = GetSaveVmBySomeId(id); // do more work with saveVm and anotherSaveVm // clear the existing model ModelState.Clear(); return View(anotherSaveVm); } // return origonal view model so that the user can correct their errors return View(saveVm); } 

I think that the data in the form collection contained in the request object is returned with the view. When you pass the model back to the post action method as a parameter, I believe that it is passed in the query string (see Request.QueryString). In most cases, it is best to pass only one or two elements of a primitive type or primitive respect types such as int? to the method of action. There is no need to pass the whole model, because it is already contained in the collection of the form of the Request object. If you want to learn QueryString, refer to Request.QueryString.

+3
source

Source: https://habr.com/ru/post/1484632/


All Articles