views:

802

answers:

2

I have followed the suggestion in this question...

[http://stackoverflow.com/questions/220020/how-to-handle-checkboxes-in-aspnet-mvc-forms][1]

...to setup multiple checkboxes with the same name="..." attribute and the form behaves as expected the FIRST time its submitted. Subsequent submissions of the form use the original array of Guid values instead of properly sending the new array of checked item values.

Relevant code in the view...

 <% foreach (ItemType itemType in ViewData.Model.ItemTypes) %>
        <%{ %>
        <li>
            <input id="selectedItems" name="selectedItems" type="checkbox" value="<%= itemType.Id%>" />
            <%= itemType.Description %></li>
        <%} %>

This produces a series of checkboxes, one each for each item with the value="..." attribute set to the Id of the item.

Then in my controller action, the method signature is...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SelectItems(Guid[] selectedItems)
{...}

The first time thru the method, the selectedItems array properly holds the Guid of each item selected. But subsequent submits of the form will always still contain whatever was first selected in the initial submit action, no matter what changes you make to what it checked before you submit the form. This doesn't seem to have anything to do with my code, as inspecting the selectedItems array that the MVC framework passes to the method evidences that the framework seems to always be submitting the same value over and over again.

Close browser, start again, selecet different initial checkbox on submit and the process starts all over again (the initially-selected checkbox ids are always what's in the selectedItems argument).

Assume I must be thick and overlooking some kind of caching of form values by the framework, but I would swear this didn't behave this way in Preview 5.

Driving me nuts and probably simple issue; any ideas????

+1  A: 

FWIW, here is what I do (not sure if it related):

  // please MS, stop screwing around!!!!!!!!!!!!!!!
  string r = Request.Form["r"];

Then proceed to extract the values manually from 'r'. I still use Preview 4, as they have really broken too many existing features, and not fixed reported bugs.

leppie
Its indeed a bug in the BETA1 release. (Grudingly) going with Guid[] selectedItems = Request.Form["selectedItems"].Split(...), as ugly as that looks to me in re: not leveraging the tools the framework is supposed to provide me to make this kind of plumbing code unnecessary :(
sbohlen
BTW, that snippet comes straight from my code :) I think they broke it after Preview 3.
leppie
A: 

I'm not sure what is causing your issue, but I have a WAG...

Do you RedirectToAction in your controller's Post method?

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SelectItems(Guid[] selectedItems)
{
  /* lol snip */
  return RedirectToAction("WhateverActionIsTheGetVersionOfThisPostAction");
}

It might serve to reset anything going on in the background... Again, wild-ass guess...

Will
Will:Yes, I'm doing that; same result. Also, on a hunch I set the form post action to a completely diff method (rather than one with the AcceptVerbs action filter approach) and same results. Confusion continues, sadly.
sbohlen