简体   繁体   中英

Receive null value to the controller

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 class="btn btn-primary btn-lg" asp-controller="Exposure" asp-action="ViewUser" asp-route-id="@TempData["Identifier"]">Details</a> </div>

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 class="btn btn-primary btn-lg" 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:

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM