ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Generate Auto increment number for each group in ASP.Net MVC<p>Hi bigbear,</p> <p>For inserting based on your TextBox you need to first get the&nbsp;last4number from database. Then increment the number with 1 and loop through the record upto the&nbsp;TextBox value and insert in database.</p>https://www.aspforums.net:443/Threads/799165/Generate-Auto-increment-number-for-each-group-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/799165/Generate-Auto-increment-number-for-each-group-in-ASPNet-MVC/Mon, 17 Jun 2019 07:54:58 GMTGenerate Auto increment number for each group in ASP.Net MVC<p>Hi&nbsp;bigbear,</p> <p>For inserting based on your TextBox you need to first get the&nbsp;last4number from database. Then increment the number with 1 and loop through the record upto the&nbsp;TextBox value and insert in database.</p> <p>Check this example. Now please take its reference and correct your code according to you table structure.</p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">CREATE TABLE CategoryDetails(Id INT IDENTITY PRIMARY KEY,Category VARCHAR(50),Last4Numbers CHAR(4))</pre> <p><strong><span style="text-decoration: underline;">Model</span></strong></p> <pre class="brush: csharp">public class ViewModel { public int ID { get; set; } [System.ComponentModel.DataAnnotations.Required] [System.ComponentModel.DisplayName(&#34;Part Book / Category&#34;)] public string PartBook { get; set; } public string Last4Number { get; set; } }</pre> <p><strong><span style="text-decoration: underline;">Controller</span></strong></p> <pre class="brush: csharp">public class HomeController : Controller { // GET: /Home/ public ActionResult Index() { TempData[&#34;PartBookDDL&#34;] = GetPartBooks(); TestEntities entities = new TestEntities(); TempData[&#34;Records&#34;] = entities.CategoryDetails.ToList(); return View(); } [HttpPost] public ActionResult Index(int txtNumberNeeded, string PartBook) { TempData[&#34;PartBookDDL&#34;] = GetPartBooks(); TestEntities entities = new TestEntities(); List&lt;SelectListItem&gt; positionList = GetPartBooks(); string category = positionList.Find(p =&gt; p.Value == PartBook).Text; int lastNumber = 0; if (entities.CategoryDetails.Where(x =&gt; x.Category == category).Count() &gt; 0) { lastNumber = Convert.ToInt32(entities.CategoryDetails.Where(x =&gt; x.Category == category).Max(x =&gt; x.Last4Numbers)); lastNumber++; } for (int i = lastNumber; i &lt; txtNumberNeeded + lastNumber; i++) { if (i != 9999) { entities.CategoryDetails.AddObject(new CategoryDetail { Category = category, Last4Numbers = i.ToString() }); entities.SaveChanges(); } } TempData[&#34;Records&#34;] = entities.CategoryDetails.ToList(); return View(&#34;Index&#34;); } // Get list from database. public List&lt;SelectListItem&gt; GetPartBooks() { List&lt;SelectListItem&gt; partBooks = new List&lt;SelectListItem&gt;(); partBooks.Add(new SelectListItem { Text = &#34;Category 1&#34;, Value = &#34;1&#34; }); partBooks.Add(new SelectListItem { Text = &#34;Category 2&#34;, Value = &#34;2&#34; }); partBooks.Add(new SelectListItem { Text = &#34;Category 3&#34;, Value = &#34;3&#34; }); partBooks.Add(new SelectListItem { Text = &#34;Category 4&#34;, Value = &#34;4&#34; }); return partBooks; } }</pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; Inherits=&#34;System.Web.Mvc.ViewPage&lt;_Add_Record_If_Available_MVC.Models.ViewModel&gt;&#34; %&gt; &lt;%@ Import Namespace=&#34;_Add_Record_If_Available_MVC&#34; %&gt; &lt;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; &lt;%using (Html.BeginForm(&#34;Index&#34;, &#34;Home&#34;, FormMethod.Post)) { %&gt; &lt;%:Html.LabelFor(x =&gt; x.PartBook)%&gt; : &lt;%:Html.DropDownListFor(x =&gt; x.PartBook,(IEnumerable&lt;SelectListItem&gt;) TempData[&#34;PartBookDDL&#34;], &#34;Select&#34;, new { @class = &#34;form-control&#34; })%&gt; &lt;div class=&#34;col-md-3 col-lg-3&#34;&gt; Last Number : &lt;%:Html.TextBox(&#34;txtNumberNeeded&#34;, null, new { @class = &#34;form-control&#34;, @type = &#34;number&#34; })%&gt; &lt;/div&gt; &lt;input type=&#34;submit&#34; /&gt;&lt;br /&gt;&lt;br /&gt; &lt;%} %&gt; &lt;%if (TempData[&#34;Records&#34;] != null) {%&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Category&lt;/td&gt; &lt;td&gt;Last4Numbers&lt;/td&gt; &lt;/tr&gt; &lt;%for (int i = 0; i &lt; ((List&lt;CategoryDetail&gt;)TempData[&#34;Records&#34;]).Count; i++) {%&gt; &lt;tr&gt; &lt;td&gt;&lt;%=((List&lt;CategoryDetail&gt;)TempData[&#34;Records&#34;])[i].Category %&gt;&lt;/td&gt; &lt;td&gt;&lt;%=((List&lt;CategoryDetail&gt;)TempData[&#34;Records&#34;])[i].Last4Numbers %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% } %&gt; &lt;/table&gt; &lt;% } %&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/O7oYc0O.gif" alt="" width="272" height="403" /></p>https://www.aspforums.net:443/Threads/799165/Generate-Auto-increment-number-for-each-group-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/799165/Generate-Auto-increment-number-for-each-group-in-ASPNet-MVC/Tue, 18 Jun 2019 06:44:25 GMT