如何将选定的值分配给列表框项以便可以将其删除?

时间:2015-05-12 14:27:46

标签: c# asp.net asp.net-mvc listbox

我目前正在开发一个列表框,它需要具有从列表框中删除/删除项目以及最终数据库的功能。当在列表框中选择一个项目(RegimeItem)时,按下删除按钮,该项目将从数据库中删除,这就是我想要发生的事情。

控制器的RemoveExercises方法中foreach语句的RequestedSelected部分当前为null,我想知道的是我如何才能使它不是?据我所知,当在列表框中选择未被识别为RequestedSelected

的项目时

控制器

    [HttpGet]
    public ActionResult ExerciseIndex(int? id, UserExerciseViewModel vmodel)
        {
            User user = db.Users.Find(id);
            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, RegimeItem regimeItem)
        {
            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 RemoveExercises(UserExerciseViewModel model, int? id)
    {
        foreach (int selected in model.RequestedSelected)
        {
            RegimeItem item = model.RequestedExercises.FirstOrDefault(i => i.RegimeItemID == selected);
            if (item != null)
            {
                User user = db.Users.Find(id);
                user.RegimeItems.Remove(item);
            }                
        }
        db.SaveChanges();
        //RedirectToAction("ExerciseIndex");
    }

        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)
        {
            return db.Users
            .Where(u => u.UserID == user.UserID)
            .SelectMany(u => u.RegimeItems)
            .ToList();
        }

模型

 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; }
    }

视图模型

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; }
}

查看(仅限细分)

      <input type="submit" name="remove"
             id="remove" value="<<" />
  </td>
  <td valign="top">
      @Html.ListBoxFor(model => model.RequestedSelected, new MultiSelectList(Model.RequestedExercises, "RegimeItemID", "RegimeExercise.Name", Model.RequestedSelected))
  </td>

更新:感谢Chris的帮助,我在控制器上取得了进展,并且我也更新了视图。它仍然没有删除单个项目,它返回一个空白列表,它也不会保存到数据库中。

1 个答案:

答案 0 :(得分:1)

你永远不会坚持任何事情。除非你在某个地方拨打SaveChanges,否则你正在做的所有事情都会在下一次请求时消失。