MVC5加1值有效,但正确添加多个设置

时间:2019-06-18 17:31:21

标签: c# entity-framework model-view-controller asp.net-mvc-5

我有一个程序,其中添加了部件号,它对于1值非常有用。我需要检查last4numbers列以查看其是否为9999,如果没有,我可以添加它。

我的视图中有一个文本框,因此您可以选择要添加此零件号的次数,每次将为last4numbers +1。

只要它不是9999,就可以添加部件号。我有一个循环,它将使用该数字来查看需要添加多少次。

我检查是否为9999,如果不能,则可以添加,但是在当前部分中要添加其设置,然后在下一个循环中,我需要将值添加1。

我的代码设置不正确,它也会自动在开头添加1。

我应该如何设置并执行此操作?这是我所拥有的:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult TakeOutPart(PartNumberViewModel model)
        {
            if (ModelState.IsValid)
            {
                var valDescription = "";
                var valUnit = "";
                var valNextNum = Request.Form["txtNextPart"];
                var valGroup = Request.Form["txtjsongroup"];
                var valLast4 = Request.Form["txtjsonlast4"];
                var valAmount = Request.Form["txtNumberNeeded"];

                if(valNextNum == "")
                {
                    if(model.PartBook != null || model.PartBook != "")
                    {
                        int.TryParse(model.PartBook, out int parseId);

                        var book = (from pb in context.PartBooks
                                    where pb.ID == parseId
                                    select pb).FirstOrDefault();

                        if(book != null)
                        {
                            var recordsPNum = (from pn in context.PartNumbers
                                               where pn.PartGroup == book.Series
                                               orderby pn.Last4Number descending
                                               select pn).Take(10).ToList();

                            var recordBiggestPN = recordsPNum.FirstOrDefault();

                            if(int.TryParse(recordBiggestPN.Last4Number, out int last4))
                            {
                                valGroup = recordBiggestPN.PartGroup;
                                var nextNumber = last4 + 1;
                                valLast4 = nextNumber.ToString();
                            }
                            else    /* FIGURE OUT HOW TO GET NEXTNUMBER IF 'NAN' (ex. G41) */
                            {
                                //model.PartGroup = recordBiggestPN.PartGroup;
                                //model.Last4Number = recordBiggestPN.Last4Number;
                                valGroup = recordBiggestPN.PartGroup;
                                valLast4 = recordBiggestPN.Last4Number;
                            }

                            valNextNum = valGroup + "." + valLast4;
                        }
                    }
                    else
                    {
                        //Error Getting PartBook
                        TempData["Error"] = "Couldn't find PartGroup|Series|ParkBook number";
                        return RedirectToAction("TakeOutPart");
                    }
                }

                // Update to other method in future (Whole DROPDOWNLIST [Value/Text])
                if(model.Description != null || model.Description != "")
                {
                    // Currently have 'DESCRIPTION ID' lets change it to TEXT
                    if(int.TryParse(model.Description, out int descID))
                    {
                        valDescription = GetDescriptionNoun(descID);
                    }
                }

                // Update to other method in future (Whole DROPDOWNLIST [Value/Text])
                if(model.Units != null || model.Units != "")
                {
                    // Currently have 'UNIT ID' lets change it to TEXT
                    if(int.TryParse(model.Units, out int unitID))
                    {
                        valUnit = GetUnitsSelection(unitID);
                    }
                }
                //else
                //{
                //    valUnit = null;
                //}

                PartNumber partNumber = new PartNumber
                    {
                        PartGroup = valGroup,                        
                        //PartGroup = model.PartGroup,
                        Last4Number = valLast4,
                        //Last4Number = model.Last4Number,
                        PartNumber1 = valNextNum,

                        DateEntered = model.DateEntered,
                        EnteredBy = model.EnteredBy,
                        SoftwareType = model.SoftwareType,
                        UsedOnAssembly = model.UsedOnAssembly,
                        GlobalPart = model.GlobalPart,
                        Source = model.Source,
                        Manufacturer = model.Manufacturer,
                        ManufacturerPartNumber = model.ManufacturerPartNumber,
                        Description = valDescription,
                        Usage = model.Usage,
                        ReqOnProject = model.RequiredProject,
                        Quantity = model.Quantity,
                        Units = valUnit, // ?? Convert.DBNull.ToString(),
                        MachineTypes = model.MachineTypes,
                        Comment = model.Comments,
                        Released = false
                    };

                if (valLast4 != "9999" || valLast4 != null || valLast4 != "" || !string.IsNullOrEmpty(valLast4))
                {
                    if(valAmount == "1")
                    {
                        if (CheckValidPartNumber(partNumber.PartNumber1))
                        {
                            context.PartNumbers.Add(partNumber);
                            context.SaveChanges();
                            TempData["Success"] = "PartNumber Added Successfully";
                            return RedirectToAction("Index");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Multiples");
                        int counter = Convert.ToInt32(valAmount);
                        int nextLast4 = 0;
                        string nextPartNum = "";

                        for(int i=0; i<=counter; i++)
                        {
                            if (CheckValidPartNumber(partNumber.PartNumber1))
                            {
                                if(int.TryParse(partNumber.Last4Number, out int last4))
                                {

                                }
                                else  /* FIGURE OUT HOW TO GET NEXTNUMBER IF 'NAN' (ex. G41) */
                                {

                                }
                            }
                            else
                            {
                                TempData["Error"] = "This PartNumber already EXISTS in the Database.";
                                break;
                            }
                        }

                    }
                }

            }

            CreatePartBookSelectList();
            CreateEnteredBySelectList();
            CreateSoftwareTypeSelectList();
            CreateSourceSelectList();
            CreateManufacturerSelectList();
            CreateDescriptionSelectList();
            CreateUsageSelectList();
            CreateUnitsSelectList();
            model.ListMachTypes = GetMachineTypes();

            return View(model);
        } 


  public bool CheckValidPartNumber(string partNumber)
        {
            bool isValid = false;

            if(partNumber != null || partNumber != "")
            {
                var newNumber = context.PartNumbers.Where(x => x.PartNumber1.Equals(partNumber)).FirstOrDefault();

                if (newNumber == null)
                {
                    isValid = true;
                }
                else
                {
                    isValid = false;
                    TempData["Error"] = "This PartNumber already EXISTS in the Database.";
                }
            }

            return isValid;
        }

0 个答案:

没有答案