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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想将foreach包装在if语句中,检查是否
var devices = collection["devices"];
Run Code Online (Sandbox Code Playgroud)
是空的.如果它为空,则不应执行每个.对于记录,集合["devices"]是表单中复选框值的集合.