Reputation: 317
Hi I have below stored procedure :
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
And I am calling this strored procedure in my index action like this :
var listautomation = orderdata.uspApp_SelectListAutomation(id, userid).ToList();
ViewData["listresults"] = listautomation;
Now I need to pass this into my view and have to display Automation_notes, Automation_recepientEmail and Automation_frequency.
Below is my static code i have written :
<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>
Can some one tell me how can i make it dynamic by taking results from Stored procedure and pass it in my view ??
Upvotes: 0
Views: 3996
Reputation: 17182
You Model and ViewModel should be -
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; }
}
Your Controller should be -
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);
}
You view should be -
@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>
Output -
Upvotes: 1
Reputation: 197
First you pass your viewmodel from controller like this
public ActionResult ActionName()
{
//your code
return View(listautomation);
}
then bind it in your view part like this
@model ViewModel.ListAutomation
Get the value in view like this
<input type="text" id="id" value="@Model.ListAutomation " readonly="True"/>
Upvotes: 0