public ActionResult Create(FormCollection collection, FormCollection formValue)
{
try
{
Project project = new Project();
TryUpdateModel(project, _updateableFields);
var devices = collection["devices"];
string[] arr1 = ((string)devices).Split(',');
int[] arr2 = Array.ConvertAll(arr1, s => int.Parse(s));
project.User = SessionVariables.AuthenticatedUser;
var time = formValue["Date"];
project.Date = time;
project.SaveAndFlush();
foreach (int i in arr2)
{
Device d = Device.Find(i);
d.Projects.Add(project);
d.SaveAndFlush();
}
return RedirectToAction("Index");
}
catch (Exception e)
{
return View(e);
}
}
我想将foreach语句包裹在if语句中,以检查是否
var devices = collection["devices"];
判断是否为空。如果它是空的,则不应执行for each循环。为了记录,collection["devices"]是表单中复选框值的集合。
IMongoCollection
,则需要使用YourCollection.AsQueryable().Count() == 0
。 - Mauricio Gracia Gutierrez