getting the values from a nested complex object that is passed to a partial view

后端 未结 4 958
感动是毒
感动是毒 2020-11-22 12:32

I have a ViewModel that has a complex object as one of its members. The complex object has 4 properties (all strings). I\'m trying to create a re-usable partial view where

4条回答
  •  Happy的楠姐
    2020-11-22 12:48

    I came across the same situation and with the help of such informative posts changed my partial code to have prefix on generated in input elements generated by partial view

    I have used Html.partial helper giving partialview name and object of ModelType and an instance of ViewDataDictionary object with Html Field Prefix to constructor of Html.partial.

    This results in GET request of "xyz url" of "Main view" and rendering partial view inside it with input elements generated with prefix e.g. earlier Name="Title" now becomes Name="MySubType.Title" in respective HTML element and same for rest of the form input elements.

    The problem occurred when POST request is made to "xyz url", expecting the Form which is filled in gets saved in to my database. But the MVC Modelbinder didn't bind my POSTed model data with form values filled in and also ModelState is also lost. The model in viewdata was also coming to null.

    Finally I tried to update model data in Posted form using TryUppdateModel method which takes model instance and html prefix which was passed earlier to partial view,and can see now model is bound with values and model state is also present.

    Please let me know if this approach is fine or bit diversified!

提交回复
热议问题