1

I have a cshtml file that has the section of similar to below code below:

  <div id="entries" class="records-table">
      @if (Model.Entries != null)
      {
          foreach (var entry in Model.Entries)
          {
              @Html.Partia("../Partials/_PartialEntry", entry)
          }
      }
  </div>

And in my _PartialEntry file I have code similar as below:

<input asp-for="SomeModelValue" />
<div class="@(Model.SomeModelValue == "Special":"hidden":string.Empty)">...</div>

The issue that I am having is for a given entry the SomeModelValue properties between asp-for="SomeModelValue" and @Model.SomeModelValue are not the same. The @Model.SomeModelValue always returns the SomeModelValue of the previous entry. I will try and provide an example below.

Lets say me Model.Entries had the values below:

[{SomeModelValue = "Special", ....}, 
 {SomeModelValue = "SomeOtherValue", ....} 
 {SomeModelValue = "Special", ....},
 {SomeModelValue = "SomeValue", ....},
 {SomeModelValue = "Special", ....}]

Note: Just using being lazy it is C# List of objects where SomeModelValue is a string property.

And I was edit the _ParialEntry code to:

<input asp-for="SomeModelValue" />
<div>@Model.SomeModelValue</div>
<div class="@(Model.SomeModelValue == "Special":"hidden":string.Empty)">...</div>

The resulting html that gets generated is similar to:

<div id="entries" class="records-table">
    ...
    <input value="Special" />
    <div>{Special}</div>
    <div {class="hidden"}>...</div>
    ...
    <input value="SomeOtherValue" />
    <div>Special{SomeOtherValue}</div>
    <div class="hidden"{} >...</div>
    ...
    <input value="Special" />
    <div>SomeOtherValue{Expecting:Special}</div>
    <div {lass="hidden"}>...</div>
    ...
    <input value="SomeValue" />
    <div>Special{SomeValue}</div>
    <div class="hidden"{}>...</div>
    ...
    <input value="Special" />
    <div>SomeValue{Special}</div>
    <div {class="hidden"}>...</div>
    ...
</div>

The expected result I have placed in {...} inother words SomeValue{Special} means I got SomeValue displayed but I expected to see Special.
I hope this give you an idea of what I am encounter, I don't seem to understand why this is happening any pointer would be helpful thank you.

Heinrich
  • 2,144
  • 3
  • 23
  • 39
  • They will be the same, unless the value has been added to `ModelState`, for example you have posted a form and changed the value then returned the view (which would be the wrong approach). Refer [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation. You need to edit your question to explain what your actually doing, including showing the controller methods. –  Aug 18 '17 at 04:42
  • And since you cannot use a `foreach` loop with a partial to generate form controls for binding (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) its not clear what you would be trying to achieve with this –  Aug 18 '17 at 04:45

0 Answers0