![](/img/trans.png)
[英]How do I display multiple tables in ASP.NET MVC view from a multi-table output stored procedure
[英]How do I pass list results from stored procedure into ASP.NET MVC view?
嗨,我有以下存儲過程:
ALTER PROCEDURE [dbo].[uspApp_SelectListAutomation]
@id int,
@UserID int
AS
BEGIN
SET NOCOUNT ON;
SELECT Automation_notes, Automation_recepientEmail, Automation_frequency
FROM Appmarket_AutomatedReports
WHERE UserID = @UserID AND id = @id
END
我在這樣的索引操作中調用此受支持的過程:
var listautomation = orderdata.uspApp_SelectListAutomation(id, userid).ToList();
ViewData["listresults"] = listautomation;
現在,我需要將其傳遞到視圖中,並顯示Automation_notes , Automation_recepientEmail和Automation_frequency 。
下面是我寫的靜態代碼:
<li style="border-left: 2px solid red;"><a href="Index/1">
<div class="col-14">
<h5>
**Automation Notes**
</h5>
<div class="stats">
( RECIPIENT: **Automation_recepientEmail** | EVERY **Automation_frequency** | EXPIRES: 19 January 2025 )
</div>
</div>
<div class="clear">
</div>
</a></li>
有人可以告訴我如何通過從存儲過程中獲取結果並將其傳遞到我的視圖中來使其動態嗎?
您的Model和ViewModel應該是-
public class ViewModel
{
public List<DataModel> Items { get; set; }
}
public class DataModel
{
public string Automation_notes { get; set; }
public string Automation_recepientEmail { get; set; }
public string Automation_frequency { get; set; }
}
您的控制器應為-
public ActionResult Index()
{
// Here you need to get data from SQL and populate the properties accordingly, I mean you need to
// call buisness layer method here
ViewModel model = new ViewModel();
model.Items = new List<DataModel>();
model.Items.Add(new DataModel() { Automation_notes = "Note1", Automation_frequency = "10", Automation_recepientEmail = "Eamil1" });
model.Items.Add(new DataModel() { Automation_notes = "Note2", Automation_frequency = "20", Automation_recepientEmail = "Eamil2" });
return View(model);
}
您認為應該是-
@model MVC.Controllers.ViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table class="table">
<tr>
<th></th>
</tr>
@foreach (var item in Model.Items) {
<tr>
<td>
@Html.Label(item.Automation_frequency)
</td>
<td>
@Html.Label(item.Automation_notes)
</td>
<td>
@Html.Label(item.Automation_recepientEmail)
</td>
</tr>
}
</table>
輸出-
首先,您像這樣從控制器傳遞視圖模型
public ActionResult ActionName()
{
//your code
return View(listautomation);
}
然后像這樣將其綁定到您的視圖部分中
@model ViewModel.ListAutomation
這樣獲得價值
<input type="text" id="id" value="@Model.ListAutomation " readonly="True"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.