小编Prd*_*Prd的帖子

检查集合是否为空

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"]是表单中复选框值的集合.

c# collections asp.net-mvc

9
推荐指数
3
解决办法
5万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

collections ×1