繁体   English   中英

如何将列表结果从存储过程传递到ASP.NET MVC视图中?

[英]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_notesAutomation_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>

有人可以告诉我如何通过从存储过程中获取结果并将其传递到我的视图中来使其动态吗?

您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM