问题
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.
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
Controller
[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();
}
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; }
}
View(Segment only)
<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.
回答1:
You're never actually persisting anything. Unless you call SaveChanges
somewhere, all of what you're doing just goes away on the next request.
来源:https://stackoverflow.com/questions/30193866/how-to-assign-a-selected-value-to-a-listbox-item-so-it-can-be-removed