Spot the bug #2


Written on Thursday, September 02, 2010 by
I came across this issue just at quitting time yesterday and was blown away when I realized what was happening. The UsersController Index View (pre submit) The UsersController Code
    public class UsersController : Controller
    {

        List<string> users = new List<string> ()
            {
                "mace.windu",
                "yoda",
                "senator.amidala",
                "anakin.skywalker",
                "obiwan.kenobi"
            };

        public ActionResult Index()
        {
            return View( users );
        }


        [HttpPost]
        public ActionResult Delete ( string[] userstodelete )
        {

            if ( userstodelete == null || userstodelete.Length == 0 )
            {
                throw new ArgumentException ( 
                    "argument must contain at least one entry", 
                    "userstodelete" );
            }

            // code could go here to
            // call out to some service to
            // delete these users

            TempData["deletedUsers"] = userstodelete;

            foreach ( var user in userstodelete )
                users.Remove ( user );

            return View ("Index", users);
        }
    }
Problem Looks like it should all work perfectly right? That's what I thought. However, clicking "Delete Users" will only "delete" our pre-darth user "anakin.skywalker". Why? Hint: Everything here is working exactly as it should.
Back

Let's talk about this

 


The opinions expressed herein are my own and do not represent my employer's view in any way.


Creative Commons License