private JsonResult GetUpDT(DataSourceRequest request, bool errorFlag, SubBuildEditModel build)
        {
            using (AdmShipDataContext dc = new AdmShipDataContext())
            {
                if (build != null)
                {
                    var ls = dc.GetBuildSubDiv(build.InvNumber, build.SubDiv);

                    if (ls.Any())
                    {
                        var b = ls.First();
                        build.buildId         = b.id;
                        build.buildNumber     = b.buildNumber;
                        build.buildName       = b.buildName;
                        build.subdivisionName = b.subdivisionName;
                        build.subdivId        = b.subdivId;
                    }
                }

                return(Json(new[] { build }.ToDataSourceResult(request, ModelState)));
            }
        }
        private ActionResult UpdateCreate(DataSourceRequest request, SubBuildEditModel build, string type)
        {
            bool errorFlag = false;

            if (ModelState.IsValid)
            {
                using (AdmShipDataContext dc = new AdmShipDataContext())
                {
                    if (build == null)
                    {
                        ModelState.AddModelError("EmptyField", "Не заполнено одно или несколько из обязательных полей!");
                        errorFlag = true;
                        return(GetUpDT(request, errorFlag, build));
                    }

                    if (String.IsNullOrEmpty(build?.InvNumber))
                    {
                        ModelState.AddModelError("EmptyBuild", "Не выбрано здание!");
                        errorFlag = true;
                        return(GetUpDT(request, errorFlag, build));
                    }

                    if (build.SubDiv == 0)
                    {
                        ModelState.AddModelError("EmptySubdiv", "Не выбрано подразделение!");
                        errorFlag = true;
                        return(GetUpDT(request, errorFlag, build));
                    }

                    if (!subdivs.Any(r => r.Id == build.SubDiv))
                    {
                        errorFlag = true;
                        ModelState.AddModelError("NotExistSubdiv", "Такого подразделения не существует!");
                        return(GetUpDT(request, errorFlag, build));
                    }

                    if (!builds.Any(r => r.InvNumber == build.InvNumber))
                    {
                        errorFlag = true;
                        ModelState.AddModelError("NotExistBuild", "Такого здания не существует!");
                        return(GetUpDT(request, errorFlag, build));
                    }

                    var ls = dc.GetBuildSubDiv(build.InvNumber, build.SubDiv);

                    if (ls.Any())
                    {
                        errorFlag = true;
                        ModelState.AddModelError("Exists", "Такая связь существует!");
                        return(GetUpDT(request, errorFlag, build));
                    }


                    var b = builds.First(r => r.InvNumber == build.InvNumber);

                    try
                    {
                        dc.SubdivBuildEdit(build.buildId, build.SubDiv, b.Name, b.InvNumber, type);
                    }
                    catch (Exception e)
                    {
                        errorFlag = true;
                        ModelState.AddModelError("Error", e.Message);
                    }
                }
            }

            return(GetUpDT(request, errorFlag, build));
        }