Invalid model property value displayed in partial view

I have a strongly typed partial view whose model contains a property with the same name as the view model of the parent page. For some reason, the rendering engine displays the value of the parent view model, rather than the expected value (well, the value that I expect at least!)

Instance of the source page view model:

public class ParentPageViewModel { public int Id { get; set; } // problem property ... public IEnumerable<ChildViewModel> Children { get; set; } } 

Extract of the child page view model:

 public class ChildViewModel { public int Id { get; set; } // problem property ... } 

Parent Page Instance (Razor):

 @model ParentPageViewModel ... @foreach (var item in Model.Children) { @Html.Partial("MyPartialView", item) } ... 

Partial View Instance:

 @model ChildViewModel ... <form ...> @Html.HiddenFor(m => m.Id) // problem here - get ParentPageViewModel.ID not ChildViewModel.Id </form> ... 

So basically in my visualized release, my hidden field has the value of the model element of the parent view, and not the value passed to the partial view. This is definitely caused by the name, since changing the name of @ ChildViewModel.Id @ to something like @ ChildViewModel.ChildId @ makes it work properly. Interestingly, when checking the values ​​of the view model in the debugger, I see the correct values; this is only processed output that is erroneous.

Is there a way around this or the β€œright” way to do what I'm trying to do (I process mini forms in the table to check ajax / post updates in the rows of the table)

Thanks,

Tim

+6
source share
4 answers

I think changing your call to this question will solve the problem:

 @Html.Partial("MyPartialView", item, new ViewDataDictionary()) 

The child view selects a value from the ViewData dictionary - so it goes into the new dictionary to the child view (hattip danludwig).

+13
source

Create a file called ChildViewModel.cshtml in Views / Shared / EditorTemplates. Put your partial view in this file:

in ~ / Views / Shared / EditorTemplates / ChildViewModel.cshtml

 @model ChildViewModel ... <form ...> @Html.HiddenFor(m => m.Id) </form> ... 

Then do this:

 @model ParentPageViewModel ... @foreach (var item in Model.Children) { @Html.EditorFor(m => item) } ... 

Or, if you prefer to save the view as partial rather than as an editor template, use Simon's answer .

0
source

Found a solution simply by creating a hidden field manually, for example:

 <input type="hidden" name="Id" value="@Model.Id" /> 

instead of using Html.HiddenFor.

(I will not mark this as an answer for a while if there are any other solutions, or anyone can explain the problem)

0
source

I know this old post. But I thought that when I landed here, when I ran into the same problem, I could also contribute. My problem was a little different. In my case, the identifier of the main view was incorrect after the action of the partial view was called, which required updating the entire page / view. I solved the problem with ModelState.Clear

 ModelState.Clear(); return View("Maintenance", model); //call main view from partial view action 
0
source

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


All Articles