在我的應用程式中,我從 Controller 向 HTML 視圖傳遞值和文本。
我還想顯示另一個額外的欄位,所以我想不要為當前的代碼而煩惱,而是從 url.Action 方法將該資料獲取到標簽。
我想知道這可以在 asp.net 中做到嗎?
到目前為止,這是我的代碼。需要幫助才能完成代碼。場景是,如果請求有先前的結算,我將該資料加載到具有請求名稱和請求 ID 的視圖中。還需要進行修改并顯示以前的審批人
這是我的 HTML 代碼。
<table class="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>
這是控制器,如果可能的話,我想從這里將 empName 傳遞給視圖并顯示它。
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;
}
}
uj5u.com熱心網友回復:
如果您不能在視圖模型中包含資料,那么您需要發出 AJAX / FETCH請求來加載它。
例如:
<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>
您還需要更改您的操作:
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/381065.html
標籤:C# 网站 asp.net-mvc
