Unable to get the the right value using generic list

86 Views Asked by At

Got a problem here with generic list. I am trying to make a site where you can select items using checkboxlist, and recieve what you selected. I am as good as finish, but I can't get the right value from the "ImageSrc"-field. I get the "Name"-value in both fields when I am printing it out on the other side.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class filmerHovedside : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {


            movies();
        }

        if (Session["Movies"] == null)
        {
            btnDeleteSession.Visible = false;
        }
        else
        {
            btnDeleteSession.Visible = true;
        }
    }

    protected void movies() {
        List<Movie> movieList= new List<Movie>();
            movieList.Add(new Movie() { Name = "Avengers", ImageSrc= "avengers.jpg" });
            movieList.Add(new Movie() { Name = "Pacific Rim", ImageSrc= "pacificRim.jpg" });
            movieList.Add(new Movie() { Name = "Star Trek", ImageSrc= "starTrek.jpg" });
            movieList.Add(new Movie() { Name = "Superman", ImageSrc= "superman.jpg" });
            movieList.Add(new Movie() { Name = "World War Z", ImageSrc= "worldwarz.jpg" });

            foreach (Movie movies in movieList)
            {
                CheckBoxList1.Items.Add(movies.Name);
            }
    }

    public List<Movie> movieList
    { 
        get 
        { 
            if (Session["Movie"] == null) 
            { 
                List<Movie> movieList= new List<Movie>();
                Session.Add("Movies", movieList); 
            } 
            return Session["Movies"] as List<Movie>; 
        } 
        set 
        { 
            Session["Movies"] = value; 
        } 
    }

    protected void btnSelectMovie_Click(object sender, EventArgs e)
    {

        if (CheckBoxList1.Items.Cast<ListItem>().Any(item => item.Selected))
        {
            Session["Movies"] = null;
            List<Movie> movies= movieList;
            foreach (ListItem item in CheckBoxList1.Items)
            {
                if (item.Selected)
                {
                    Movie newMovie = new Movie() { Name = item.Value, ImageSrc = item.Value };
                    movies.Add(newMovies);
                }
            }

            Response.Redirect("movieCeck.aspx");

        }
        else
        {
            StatusLiteral.Text = "Choose at least one movie";
        }


    }

    protected void btnDeleteSession_Click(object sender, EventArgs e)
    {
        Session["Movie"] = null;
        Response.Redirect("~/movieIndex.aspx");
    }
}
2

There are 2 best solutions below

1
On

It looks like you're setting both Name and ImageSrc to item.Value.

2
On

Probably because of mistype overhere if (Session["Movie"] == null)

it should be if (Session["Movies"] == null)