简体   繁体   中英

How do I get HTML form data in Partial View to be serialized when parent view has same model?

I have a view that contains several partial views with different models, all except one, which has the same model as the parent view. The reason being, parent view represents a Risk, and the partial view is loaded based on whether the Risk is "Motor" or "Non-Motor". Both Motor and Non-Motor partial views use the same model found in the Risk parent view.

I'm using the Html.BeginCollectionItem method and serializing the form data when submitting, in an attempt to retrieve said data in the same class used for the parent view's model (Risk). The submission manages to collect all data except for that found in the Motor and Non-Motor partial views. The goal was for the data in the Motor/Non-Motor partial views to be serialized in the same object as the Risk model in the parent view, however, when I debug the receiving method, all attributes collected in the partial views are either null or 0, although values were in fact entered in the form.

How do I get the Risk item to have all data, rather than just the data entered outside of the partial views?

If you can, please provide code snippets (and additional information as to what you want the parent view & partial views to accomplish) in order for us to get a better understanding of the problem you are facing and provide more accurate answers.

From what I can gather:

  • You have a view page that contains several partial views, but these partial views you are attempting to wrap within the parent view's form, and when attempting to submit, you are getting nulls for the values within those two partialViews?

Without understanding the business context behind why you are designing the app this way, here are my suggestions:

  1. Create a ViewModel class that has a property of type Risk class, a property of type Risk (for your Motor partialView), and a property of type Risk (for your non-motor partialView).
  2. Or, if you need to submit just specific sections (such as one partialView displayed on the parent View), you could wrap that partialView in another form element (Ex. @Html.BeginForm{} or @Ajax.BeginForm{}) and have them submit separately.

So I found a solution that works. Turns out when the data was serialized, the attributes of the Motor risk were serialized at the root level of the object. So whereas it should have been serialized as Policy.Policy_Risk.risk_item_no for example, it was only being serialized as risk_item_no, which conflicts with the Policy object that I'm receiving in the controller.

I fixed it by adding a name attribute to each item in the form in a manner as @name="Policy_Risk" + unique_id + ".risk_item_no"

That worked out, thanks for the help all the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM