How can i get the correct information to display in my listbox?

Nilmag

I have two Listboxes where you can move exercises from an available list of exercises to a list of selected exercises for a specific user. My current problem is that my selected list (aka RegimeItems) shows the exercises for all of the users.

I believe the problem lies in ChosenExercises(at the bottom of controller) i am also getting a null reference exception at:

var regimeIDs = model.SavedRequested.Split(',').Select(x => int.Parse(x));

Controller.cs

    [HttpGet]
            public ActionResult ExerciseIndex(int? id, UserExerciseViewModel vmodel)
            {
                User user = db.Users.Find(id);
                user.RegimeItems = ChosenExercises();
                UserExerciseViewModel model = new UserExerciseViewModel { AvailableExercises = GetAllExercises(), RequestedExercises = ChosenExercises(user, vmodel) };
                user.RegimeItems = model.RequestedExercises;
                return View(model);
            }

            [HttpPost]
            public ActionResult ExerciseIndex(UserExerciseViewModel model, string add, string remove, string send, int id)
            {
                User user = db.Users.Find(id);
                user.RegimeItems = model.RequestedExercises;;
                RestoreSavedState(model);
                if (!string.IsNullOrEmpty(add))
                    AddExercises(model, id);
                else if (!string.IsNullOrEmpty(remove))
                    RemoveExercises(model, id);        
                SaveState(model);
                return View(model);
            }

            void SaveState(UserExerciseViewModel model)
            {
                model.SavedRequested = string.Join(",", model.RequestedExercises.Select(p => p.RegimeItemID.ToString()).ToArray());
                model.AvailableExercises = GetAllExercises().ToList();
            }

            void RestoreSavedState(UserExerciseViewModel model)
            {
                model.RequestedExercises = new List<RegimeItem>();

                //get the previously stored items
                if (!string.IsNullOrEmpty(model.SavedRequested))
                {
                    string[] exIds = model.SavedRequested.Split(',');
                    var exercises = GetAllExercises().Where(p => exIds.Contains(p.ExerciseID.ToString()));
                    model.AvailableExercises.AddRange(exercises);
                }
            }

            private List<Exercise> GetAllExercises()
            {
                return db.Exercises.ToList();
            }

            private List<RegimeItem> ChosenExercises(User user, UserExerciseViewModel model)//RegimeItem regimeItem)
            {
var regimeIDs = model.SavedRequested.Split(',').Select(x => int.Parse(x));
            return db.RegimeItems.Where(e => regimeIDs.Contains(e.RegimeItemID)).ToList();
            //return db.Users.Where(r => r.RegimeItems = user.UserID);
            //return db.Users.Where(r => r.RegimeItems.RegimeItemID == user.UserID);
            }

Models

  public class User
    {
        public int UserID { get; set; }
        public ICollection<RegimeItem> RegimeItems { get; set; }
        public User()
        {
            this.RegimeItems = new List<RegimeItem>();
        } 
    }
    public class RegimeItem
    {
        public int RegimeItemID { get; set; }
        public Exercise RegimeExercise { get; set; }
    }

ViewModel

public class UserExerciseViewModel
{
    public List<Exercise> AvailableExercises { get; set; }
    public List<RegimeItem> RequestedExercises { get; set; }
    public int? SelectedExercise { get; set; }
    public int[] AvailableSelected { get; set; }
    public int[] RequestedSelected { get; set; }
    public string SavedRequested { get; set; }
}
Peter B

Try changing

return db.Users.Where(r => r.RegimeItems = user.UserID);

to

return db.Users.Where(r => r.RegimeItems.RegimeItemID == user.UserID);

Because the way I read it you're currently trying to match an int with an object.

edit: Another error was resulted which the OP found himself.

"It seems that RegimeItems is a collection of RegimeItem - it does not have a have a single ID. So it needed to be .Where(u => u.UserID == user.UserID) and then to select the regimeItems associated with the user .SelectMany(u => u.RegimeItems).ToList(); Thanks for your help, if you update your answer with that i will mark yours as the correct answer." -Nilmag

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I display all my sql tables in a ListBox

How can i get my ListBox working correctly

How can I get the information in the correct form (Spring Boot)

How can I fix my code to display correct index of array

How do I get my timeStamp variable to display the correct date?

How can I get information about the file that launched my app?

How can I get my code to return the correct variable?

How can I use my helper functions to get the correct output

How can I get this Collapsing Bar to display with my Google map?

How can I get my table to display grouped data properly?

How can I get the button text to display my cell text?

How can i get my website to display as https://www

How can I get Outlook to display my tables properly

How can I get my view to display the property?

How can I get the S/N of my laptop display screen?

How can i get my deck to display ? It keeps saying null

How can I get my function to display text over an image?

How can I get consistent length for all the attributes and also the correct information when compared to the detail page

How can I display TimeSpan in correct format?

How can I display the IDI_INFORMATION icon at 16 x 16 in the lower left of my dialog?

How do I get my contact form emails to work and display information?

How can I display text on the back of my card in the correct writing direction?

How do I get Listbox to display horizontally in code programmatically?

How can I correct my Java code ?

How can I ensure I still get correct touch inputs when my scene is offset?

How can I keep updating my ListBox synchronously

How can I change IsVisible property of my ListBox?

When using queryOrderedByChild to get a top score list, how can I display tied scores in correct order?

How can I get icons to stay in background and swapped text to display correct on CSS/jQuery hover/mouseenter?