您好,我有一个包含复选框的列表,使用jquery可以检查项目ID,并将它们收集到数组中,但是循环在所有项目被检查之前结束。
这是从Url获取Array;
public class IntArrayModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value == null || string.IsNullOrEmpty(value.AttemptedValue))
{
return null;
}
return value
.AttemptedValue
.Split(';')
.Select(int.Parse)
.ToArray();
}
}
public ActionResult ApproveSelected([ModelBinder(typeof(IntArrayModelBinder))] int[] id)
{
try
{
var AllParticipants = Db.Participants
.Where(m => id.Contains(m.Id))
.OrderBy(m => m.Id)
.ToList();
for (int i = 0; i < AllParticipants.Count; i++)
{
var item = AllParticipants.First();
item.Approval = true;
var itemRemove = AllParticipants.First();
AllParticipants.Remove(item);
}
Db.SaveChanges();
}
catch
{
return Json(new { IsComplete = false });
}
return Json(new { IsComplete = true });
}
因此,如果我发送1个ID没问题。如果我发送2,如果第一个的更改批准,则留下成功消息的循环。如果我发送2,它将更改2,然后离开1。当我进行4时,它将更改2,然后离开2。当我进行5,它将更改3,然后离开2。它很尴尬。我该如何解决?
改变
for (int i = 0; i < AllParticipants.Count; i++)
{
var item = AllParticipants.First();
item.Approval = true;
var itemRemove = AllParticipants.First();
AllParticipants.Remove(item);
}
对此:
Db.Participants
.Where(m => id.Contains(m.Id))
.ToList()
.ForEach(participant=>participant.Approval=true);
Db.SaveChanges();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句