Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a real headscratcher of a problem. I have a view on that has a list of items. There's an AssignedTo on the objects. This page allows you to assign a subset of the items to various people and the unassigned items are returned in the same form.

For example:
The list on the view has 5 items, IDs 1,2,3,4,5.
If the first two of them are assigned to someone the view should re-render with 3 items, with the IDs, 3,4 and 5.

Here's the code for the View.

C#
@using (Html.BeginForm("SaveQueues", "BillPayerChanges", FormMethod.Post, null))
  {
      <ul class="table" style="margin: 0px;">
          @{
              var counter = 0;
          }


          @for (var i = 0; i < this.Model.Count; i++)
          {
              @Html.HiddenFor(m => Model[i].Id)
              <li class="row first width10"</li>
              <li class="row showDetails" style="width: 210px;" data-billpayid="@Model[i].Id"><a href="#">@Model[i].MemberId </a>
              </li>
              <li class="row" style="width: 200px">@Model[i].DateVerified.ToString("MM/dd/yyyy")</li>
              <li class="row" style="width: 200px">@Html.DropDownListFor(m => Model[i].AssignedTo, teamDropdown)</li>
              <li class="row" style="width: 100px"</li>
          }
      </ul>
      <br/>
      <input type="submit" value="Save"/>
  }



After the POST the @Html.HiddenFor will show ID 1 on the first row instead of ID 3. HOWEVER, you notice I have a data- element that shows Model[i].Id and that shows correctly with the right ID.

I'm truly stumped on this one, any help would be appreciated.

- Andrew
Posted
Comments
Jameel VM 6-Jun-13 12:42pm    
can you post the Model class that you have iterate?

Your data element is showing the MemberId. The HiddenFor is showing the Id. Apparently, these can differ, and sometimes Id is 1 when MemberId is 3.
 
Share this answer
 
Comments
AnalogNerd 6-Jun-13 12:42pm    
MemberId is different from the ID. The MemberId is their actual membership number, the Id is the primary key in the database (a member could be in this table multiple times, so MemberId couldn't be used as a primary key).

The problem is this line :
@Html.HiddenFor(m => Model[i].Id)
and this code:
data-billpayid="@Model[i].Id"

Return two different numbers in the code. The latter is correct, the HiddenFor code shows the old Id from the previous items.
Christian Graus 6-Jun-13 12:43pm    
Oh, I see, sorry. This code is a little messy. Why don't you use foreach instead ? If you do that, it might fix the underlying issue, as well as being cleaner.
AnalogNerd 6-Jun-13 12:46pm    
I actually had it as a foreach before and the same thing happens.

I did confirm that the Model being return from the action is correct, but for some reason this old Id is hanging out in the View.
Christian Graus 6-Jun-13 12:46pm    
If you use foreach and then use the object directly, then the only possible way for this to happen is if your getters actually change the value of the property. Have you set breakpoints inside the getter to see what happens there ?
AnalogNerd 6-Jun-13 13:00pm    
My getter doesn't do anything:

public int Id { get; set; }
@using (Html.BeginForm("SaveQueues", "BillPayerChanges", FormMethod.Post, null))
{


    @foreach(var item in Model)
    {
    @Html.HiddenFor(m => item.Id)
    <li class="row first width10"/>
    <li class="row showDetails" style="width: 210px;" data-billpayid="@item.Id"><a href="#">@item.MemberId </a>
    </li>
    <li class="row" style="width: 200px">@Model[i].DateVerified.ToString("MM/dd/yyyy")</li>
    <li class="row" style="width: 200px">@Html.DropDownListFor(m => item.AssignedTo, teamDropdown)</li>
    <li class="row" style="width: 100px"> </li>
    }
    </ul>
    <br />
    <input type="submit" value="Save"/>
    }
 
Share this answer
 
v2
Comments
AnalogNerd 6-Jun-13 14:07pm    
My problem with a foreach is that it was not returning my model to the action when I posted. This may have been because my model for the view was a typed List. I created a new viewmodel that has a list in it of my queues. I then did a foreach, same thing.

I then did a EditorTemplate, here's the new code in my view:
@Html.EditorFor(m => m.Changes)

and in the template:
@Html.HiddenFor(m => Model.Id)
@Model.Id

And the result:
<input id="Changes_0__Id" name="Changes[0].Id" type="hidden" value="7617" />
7618
Christian Graus 6-Jun-13 14:08pm    
You need to add some breakpoints to work out what is going on. Make a property that has a value behind it, so you can set breakpoints on your set and get and see what they are doing/returning.
I'm putting this in as a solution so I can format the code and because this did fix it, but I have no idea why. Which isn't very comforting.

The intial view that loads the queue was through an action in my controller called ManageQueues

C#
public ActionResult ManageQueues()
{
    var viewModel = new ExternalChangesMasterViewModel();
    viewModel.Changes = this.queueService.GetAllUnassigned();

    return this.View("ManageQueues", viewModel);
}


The form submitted to different action called SaveQueues, which looked like this:
C#
[HttpPost]
       public ActionResult SaveQueues(ExternalChangesMasterViewModel updatedQueues)
       {
          this.queueService.UpdateQueues(updatedQueues.Changes);
          var viewModel = new ExternalChangesMasterViewModel();
          viewModel.Changes = this.queueService.GetAllUnassigned();
          return this.View("ManageQueues", viewModel);
       }


When I changed this Action to the following, the problem went away. However, to be honest I don't see the difference between retreiving the items myself and redirecting to another action which does the same thing, so if someone has an explanation I'd love to hear it:

C#
[HttpPost]
       public ActionResult SaveQueues(ExternalChangesMasterViewModel updatedQueues)
       {
          this.queueService.UpdateQueues(updatedQueues.Changes);
          return this.RedirectToAction("ManageQueues");
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900