In my application, from Controller I'm passing value and text to the HTML view.
Also I want to show another additional field, So I thought not to struggling with the current code and get that data to the label from url.Action method.
I want to know is this possible to do in asp.net ?
Also here is my code so far I did. Need a help to complete the code. Scenario is, if the request has previous settlements, I load that data to the view with the request name and the request Id. Also need to do a modification and show the previous approver as well
Here is my HTML code.
<table >
<tr>
<th>
Settling Request Type
</th>
<th>
Request Id
</th>
<th>
Previously Approved By
</th>
</tr>
@foreach (var item in Settlements)
{
<tr>
<td>
@Html.Label(item.Text.ToString())
</td>
<td>
@Html.Label(item.Value)
@Html.ActionLink("Click to view", "Details", "AppRequests", new { id = @item.Value }, new { target = "_blank" })
</td>
<td>
<a href="@Url.Action("PreviousTopApprover", "PendingRequestM", new { id = item.Value })"></a>
</td>
</tr>
}
</table>
This is the controller and If it's possible, from here I want to pass the empName to the view and show it.
public ActionResult PreviousTopApprover(int ?Id)
{
if (Id !=null)
{
var Approver = (from appProcess in db.ApprovalProcess
join appParties in db.ApproveParties on appProcess.Id equals appParties.ApprovalProcess_Id
join emp in db.CreateEmployee on appParties.Approver_Id equals emp.Id
where appProcess.Req_Id == Id && appParties.Approve_Type == true
select new { emp.EmpName }).ToList();
string empName = Approver.First().EmpName;
return View();
}
else
{
return null;
}
}
CodePudding user response:
If you can't include the data in your view-model, then you'll need to make an AJAX / FETCH request to load it.
For example:
<td>
<span data-toggle="fetch" data-url="@Url.Action("PreviousTopApprover", "PendingRequestM", new { id = item.Value })">Loading...</span>
</td>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll("[data-toggle='fetch']").forEach(async (el) => {
const url = el.dataset.url;
const response = await fetch(url);
if (!response.ok) {
const errorMessage = await response.text();
console.error(response.status, response.statusText, errorMessage);
el.innerHTML = "Error loading approver";
return;
}
const data = await response.text();
el.innerHTML = data;
});
});
</script>
You'll also want to change your action:
public ActionResult PreviousTopApprover(int ?Id)
{
if (Id == null) return HttpNotFound();
var approver = (from appProcess in db.ApprovalProcess
join appParties in db.ApproveParties on appProcess.Id equals appParties.ApprovalProcess_Id
join emp in db.CreateEmployee on appParties.Approver_Id equals emp.Id
where appProcess.Req_Id == Id && appParties.Approve_Type == true
select new { emp.EmpName })
.FirstOrDefault();
if (approver == null) return HttpNotFound();
return Content(approver.EmpName);
}