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); } }
I want to wrap foreach in an if statement, which checks
var devices = collection["devices"];
empty or not. If its empty for each should not be executed. For the record, the ["devices"] collection is a set of checkbox values from the form.
collections c # asp.net-mvc
Prd
source share