如何从此列表框中删除项目?

时间:2015-05-06 11:26:06

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

我有一个列表框,我需要能够删除单个 RegimeItems 。这些 RegimItems 属于特定用户,但由于它们现在是列表框显示特定用户,所以我不知道是否需要在代码中说明。目前它没有显示错误,但它没有返回列表框中剩余的 RegimeItems ,也没有实际更新数据库中的更改。

更新:我在RemoveExercises的model.RequestedSelected行的foreach (int selected in model.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);
    }
    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.RequestedExercises, new MultiSelectList(Model.RequestedExercises, "RegimeItemID", "RegimeExercise.Name", Model.RequestedSelected))
  </td>

1 个答案:

答案 0 :(得分:2)

您的model.RequestedExercises列表没有RegimeItemID属性。这是一个项目列表。我怀疑你真正想要的更像是:

foreach(int selected in RequestedSelected)
{
   RegimeItem item = model.RequestedExercises.FirstOrDefault(i => i.RegimeItemID == selected);
   if(item != null)
   {
      User user = db.Users.Find(id);
      user.RegimeItems.Remove(item);
   }
}

希望这能让你走上正轨。