2014-01-22 38 views
0

您好我有下面的存儲過程:如何將存儲過程的列表結果傳遞到ASP.NET MVC視圖?

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 

而且我打電話我的索引行動像這樣這樣strored過程:

var listautomation = orderdata.uspApp_SelectListAutomation(id, userid).ToList(); 
    ViewData["listresults"] = listautomation; 

現在我需要這個傳遞到我的觀點,並有顯示Automation_notes,Automation_recepientEmailAutomation_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> 

有沒有人告訴我,我怎樣才能使它動態,採取從存儲過程的結果,並通過它,我認爲?

回答

0

首先,從控制器通過您的視圖模型這樣

public ActionResult ActionName() 
     { 
      //your code 
      return View(listautomation);    
     } 

然後將其綁定在這樣

@model ViewModel.ListAutomation 

您的視圖部分獲取鑑於值這樣

<input type="text" id="id" value="@Model.ListAutomation " readonly="True"/> 
1

您的模型和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> 

輸出 - enter image description here

相關問題