2014-01-22 39 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

相关问题