Hi zynergiatechn,
You need to make use of TempData object instead of ViewBag.
ViewBag value will be lost after redirection.
Refer below example.
Model
public class WorkerIdentity
{
public int Id { get; set; }
[Required(ErrorMessage ="Required!")]
public string? FirstName { get; set; }
[Required(ErrorMessage = "Required!")]
public string? LastName { get; set; }
[Required(ErrorMessage = "Required!")]
public string? CompnyCode { get; set; }
[Required(ErrorMessage = "Required!")]
public string? DeptmentCode { get; set; }
public string? IdentificationKey { get; set; }
public string? WorkerIdNo { get; set; }
}
Controller
public class HomeController : Controller
{
public async Task<IActionResult> Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index([Bind("Id,FirstName,LastName,CompnyCode,DeptmentCode,IdentificationKey,WorkerIdNo")] WorkerIdentity workerIdentity)
{
if (ModelState.IsValid)
{
string? firstNme = workerIdentity.FirstName;
string? compCode = workerIdentity.CompnyCode;
string? deptCode = workerIdentity.DeptmentCode;
string? identKey = workerIdentity.IdentificationKey;
string WorkerIdentNo = string.Format("{0}.{1}.{2}.{3}", firstNme, compCode, deptCode, identKey);
TempData["WorkerIdentificationNo"] = WorkerIdentNo;
return RedirectToAction(nameof(Index));
}
return View(workerIdentity);
}
}
View
@model Sample_133694.Models.WorkerIdentity
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.2/css/bootstrap.min.css" />
</head>
<body class="container-fluid">
@Html.AntiForgeryToken()
<div class="row">
<div class="col-md-4">
<form asp-action="Index">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control" name="FirstName" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CompnyCode" class="control-label"></label>
<input asp-for="CompnyCode" class="form-control" name="CompnyCode" />
<span asp-validation-for="CompnyCode" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DeptmentCode" class="control-label"></label>
<input asp-for="DeptmentCode" class="form-control" name="DeptmentCode" />
<span asp-validation-for="DeptmentCode" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="IdentificationKey" class="control-label"></label>
<input asp-for="IdentificationKey" class="form-control" name="IdentificationKey" />
<span asp-validation-for="IdentificationKey" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="WorkerIdNo" class="control-label"></label>
<input asp-for="WorkerIdNo" class="form-control" value="@TempData["WorkerIdentificationNo"]" />
<span asp-validation-for="WorkerIdNo" class="text-danger"></span>
</div><br />
<div class="form-group">
<input type="submit" value="Identify" class="btn btn-primary" />
</div>
</form>
</div>
</div>
</body>
</html>
Screenshot
![](https://i.imgur.com/73TqWqR.gif)