Hello I'm trying to pass some database info from my controller to my view, but don't find the best way to do it. I'm populating the model in my controller, but I need to populate those values from database. I have a class called DataAccess which is the one that contains all my queries but not sure where I should put the logic to populate. I would say a for loop in my controller to populate the values, but seems to fail since I'm declaring the SchedulerViewModel there
The idea is having my values next to a radio button, so when selecting a radio button, I can "detect" the value and do something with that option....any suggestion would be appreciated...
My model:
public class SchedulerViewModel
{
public string theValue { get; set; }
public SelectListItem[] Items { get; set; }
}
My Controller:
public ActionResult Scheduler()
{
//DataAccess dataAccess = new DataAccess();
//for loop here???
var model = new SchedulerViewModel
{
Items = new[]
{
new SelectListItem { Value = "U", Text = "USA" }
}
};
return View(model);
}
My view:
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Items.Count(); i++)
{
@Html.RadioButtonFor(x => x. theValue, Model.Items[i].Value, new { id = "item_" + i })
@Html.Label("item_" + i, Model.Items[i].Text)
<br />
}
}
Ideally you would have a service class that handles your database access. You shouldn't directly invoke the data layer from the controller, although nothing prevents you from doing it. For simplicity, I'm just putting calling the data access directly in the controller. The idea is that you need to return a collection of data, here an IEnumerable, in the View at the controller level so that the View can display this data.
Controller:
[HttpGet]
public ActionResult Index()
{
KnowledgeBaseEntities context = new KnowledgeBaseEntities();
IEnumerable<ISSUE> issues = context.ISSUES;
if(issues == null)
{
return HttpNotFound();
}
return View(issues);
}
View: As you can see I'm referencing the collection of data that I'm expecting from the controller.
@model IEnumerable<ISSUE>
In this case it's an IEnumerable just like I had in the controller. Then you'll notice I'm referencing a Model object when I iterate the model.
@foreach (var item in Model)
Then I'm looping through each row of the model in order to add table rows to the table. Because we're using Model Binding from the Entity Framework. We're using Razor Syntax. You also notice I'm using Action Links for each row in the last column. This allows me to Edit, Delete or provide Details for a row of data. However, I will need to invoke another Controller Action for that. For example, you'd have an Edit controller action method that returns a single ISSUE to an Edit View.
@model IEnumerable<ISSUE>
@{
ViewBag.Title = "Knowledge Base Issues";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2 class="line">All Issues</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="flat">
<tr>
<th>@Html.DisplayNameFor(model => model.KEYWORDS)</th>
<th>@Html.DisplayNameFor(model => model.SUBJECT)</th>
<th>@Html.DisplayNameFor(model => model.DATE_ENTERED)</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.KEYWORDS)</td>
<td>@Html.DisplayFor(modelItem => item.SUBJECT)</td>
<td>@Html.DisplayFor(modelItem => item.DATE_ENTERED)</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ISSUE_ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ISSUE_ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ISSUE_ID })
</td>
</tr>
}