nabilabolo says:
id2 =
"FY21_"
+ (NewID + 1);
Youneed to parse the NewID to int onorder to add value. As in your code it is string so the 1 is appended instead of add.
Refer below sample code.
Model
public class ItemModel
{
public string req_name { get; set; }
public string badge_num { get; set; }
public string line { get; set; }
public DateTime req_date { get; set; }
public int status { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View(new ItemModel());
}
public ActionResult AddPlatform()
{
return View();
}
[HttpPost]
public ActionResult AddPlatform(ItemModel Imodel)
{
RequesterEntities db = new RequesterEntities();
TempData["Selected"] = "1";
try
{
string id = db.Requesters.Max(x => x.Requester_id);
string id2;
if (id == null)
{
id2 = "FY21_001";
}
else
{
string NewID = id.Split('_')[1];
id2 = "FY21_" + (int.Parse(NewID) + 1).ToString().PadLeft(3, '0');
}
Requester model = new Requester();
model.Requester_id = id2;
model.req_name = Imodel.req_name;
model.badge_num = Imodel.badge_num;
model.line = Imodel.line;
model.req_date = DateTime.Now;
model.status = "new";
db.Requesters.Add(model);
db.SaveChanges();
}
catch (Exception)
{
}
return RedirectToAction("Index", "Home");
}
}
View
@model Insert_String_ID_Button_Click_MVC.Models.ItemModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" media="screen" href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css' />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () {
if ($('#hfSelected').val() != '') {
$("#tabs").tabs({ active: $('#hfSelected').val() });
} else {
$("#tabs").tabs();
}
$('#btnSaveMaterial').click(function () {
$('#hfSelected').val(2);
$("#tabs").tabs({ active: $('#hfSelected').val() });
});
});
</script>
</head>
<body>
<input type="hidden" value="@TempData["Selected"]" id="hfSelected" />
<div id="tabs">
<ul>
<li><a href="#tabs-1">Details</a></li>
<li><a href="#tabs-2">Material</a></li>
<li><a href="#tabs-3">Summary</a></li>
</ul>
<div id="tabs-1">
@using (Html.BeginForm("AddPlatform", "Home", FormMethod.Post))
{
<label for="name" class="mr-sm-2">Name:</label>
<span>@Html.EditorFor(model => model.req_name, new { htmlAttributes = new { @class = "form-control mr-sm-2", style = "width:800px" } })</span>
<br />
<label for="email" class="mr-sm-2">Badge Number:</label>
<span>@Html.EditorFor(model => model.badge_num, new { htmlAttributes = new { @class = "form-control mr-sm-2", style = "width:800px" } })</span>
<br />
<label for="phone" class="mr-sm-2">Line :</label>
<span>@Html.EditorFor(model => model.line, new { htmlAttributes = new { @class = "form-control mr-sm-2", style = "width:800px" } })</span>
<br /><br />
<div align="center">
<input id="btnSaveDetails" type="submit" value="Save" class="btn btn-primary" />
</div>
}
</div>
<div id="tabs-2">
Content for Tab 2 goes here.<br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
<br />
<input id="btnSaveMaterial" type="submit" value="Save" class="btn btn-primary" />
</div>
<div id="tabs-3">
Content for Tab 3 goes here.<br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</div>
</div>
</body>
</html>
Screenshot
Inserted record