Home > front end >  Receive null value to the controller
Receive null value to the controller

Time:06-07

Controller 'ViewUser' getting null value from Tempdata["Identifier']. is there another way to pass the value to the controller?

public IActionResult Details(string Id)
        {
            objInsuredList = (from obj in _db.dbLifeData
            where obj.Identifier.Contains(Id) select obj).ToList();
            
            string strIdentifier = string.Empty;
          
            foreach (var item in objInsuredList)
            {
                strIdentifier = item.Identifier;
 
            }
            TempData ["Identifier"] = strIdentifier;
        }
        
public IActionResult ViewUser(string Identifier)
{
    objInsuredList = (from obj in _db.dbLifeData
                      where obj.Identifier.Contains(Identifier)
                      select obj).ToList();
    return View();
}
<div>
  <a  asp-controller="Exposure" asp-action="ViewUser" asp-route-id="@TempData["Identifier"]">Details</a>
</div>

CodePudding user response:

Controller 'ViewUser' getting null value from Tempdata["Identifier']. is there another way to pass the value to the controller?

Based on your description and comment it seems that you are trying to pass TempData ["Identifier"] = strIdentifier; value to your route value which is asp-route-id="@TempData["Identifier"]" and then you want to your pass that Identifier value to your controller ViewUser that is IActionResult ViewUser(string Identifier)

Proper Way To Pass Route Value:

<div>
    <a  asp-controller="Exposure" asp-action="ViewUser" asp-route-Identifier="@TempData["Identifier"]">Details</a>
</div>

Note: Replace the value with asp-route-Identifier instead of asp-route-id

Output:

enter image description here

Explanation:

Firstly, I am assigning the value kiron to TempData["Identifier"] = "Kiron";

then accessing the value to my view as asp-route-Identifier="@TempData["Identifier"]" and

finally, passing it to ViewUser Controller

  • Related