Generate Auto increment number for each group in ASP.Net MVC

Last Reply on Jun 18, 2019 06:44 AM By dharmendr

Posted on Jun 16, 2019 11:47 PM

Hello everyone,

I am splitting my question up into parts so it ll be easier for everyone including myself.

I have a database table with certain columns a group which is like a category and last4numbers which goes from 0-9999 when at 9999 the category is full.

I have a viewmodel that is bascally my db table plus a few extra properties. I have a textbox that has a number and this number is how manytimes this record needs to be added. Each record increments 1 off the last entered last4numbers. I’m using entity framework so I can call my context and do context.add(entity) to create a record.

What are some recommend ways that I can add a new record in the database which the column last4numbers is just +1 off the last record added. All the records will be in the same part group but I need to see if I have x numbers available in the table. Some records skip around so this is where the hard part comes in to make sure the number is available. I get the last4number biggest number with a list orderby Last4Number descending that gives me the biggest last one used.

        //Actual model
        public int ID { get; set; }
        [Display(Name = "Part Book / Category")]
        public string PartBook { get; set; }
        public string PartGroup { get; set; }
        public string Last4Number { get; set; }        
        public string PartNextNumber { get; set; }
        [Display(Name = "Today's Date")]
        public DateTime DateEntered { get; set; }       
        public string Manufacturer { get; set; }
        [Display(Name = "Manufacturer Part Number")]
        public string ManufacturerPartNumber { get; set; }
        public string Usage { get; set; }
        [Display(Name = "Required Project(s)")]
        public string RequiredProject { get; set; }
        [Range(0, Int32.MaxValue)]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#}")]
        public int Quantity { get; set; }
        public string Units { get; set; }


@using (Html.BeginForm())
    @Html.DropDownListFor(x => x.PartBook, (IEnumerable<SelectListItem>) ViewBag.PartBookDDL, "", new { @class = "form-control" })
    <div class="col-md-3 col-lg-3">
        @Html.TextBox("txtNumberNeeded", null, new { @class = "form-control", @type = "number" })
    <input type="submit" />


This question does not have replies marked as Answer.