如何让它可以被删除分配选定值列表框项目? [英] How to assign a selected value to a listbox item so it can be removed?

查看:178
本文介绍了如何让它可以被删除分配选定值列表框项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的工作在需要有一个函数删除/删除从列表框中,并最终在数据库项目列表框。当一个项目(RegimeItem)在列表框中选中,再删除按钮是pressed该项目是从数据库中删除,这是我希望发生的。

I am currently working on a listbox that needs to have a function to remove/delete items from the listbox and ultimately the database. When an item(RegimeItem) is selected in the listbox, then the remove button is pressed the item is deleted from the database, that is what i want to happen.

在控制器的 RemoveExercises 方法foreach语句的RequestedSelected部分是目前空,我想知道的是我如何让这个它不是' T'从我的理解是,当该项目在没有被识别为 RequestedSelected

The RequestedSelected part of the foreach statement in the RemoveExercises method of the controller is currently null, what i want to know is how do i make it so that it isn't? From what i understand is that when the item is selected in the listbox that is not recognized as 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>

更新:感谢克里斯的帮助下,我已与控制器进展,我也更新了看法。它仍然是不删除单个项目,它返回一个空列表,它不保存到数据库无论是。

UPDATE: Thanks to Chris' help i have made progress with the controller and i have updated the view also. It is still not removing individual items, it is returning a blank list and it does not save to the db either.

推荐答案

你从来没有真正坚持什么。除非你叫的SaveChanges 的地方,所有的你在做什么只是消失在一个请求。

You're never actually persisting anything. Unless you call SaveChanges somewhere, all of what you're doing just goes away on the next request.

这篇关于如何让它可以被删除分配选定值列表框项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆