I am new to MVC3 and trying to work on my first small project at work. I have a HomeController with two actions, Index() and GetData(). Index just returns the Index view. When the Index page loads, I want there to be an html Button or Input of type Button. When I click on this button, I want the GetData action to run, which selects some data based on a linq query and returns a List. Then in the Index view itself, below the button which i clicked, I want the list to be populated onto an html table .....I assume using a foreach loop.
This is not working, and I think I am clearly misunderstanding the concept of this MVC pattern.
My Controller Code:
private ReferenceDataContext data = new ReferenceDataContext();
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
using (data)
{
var hosp = (from x in data.Hospitals
where x.HAcode == "Q36"
select x).ToList();
ViewData["hosp"] = hosp;
return RedirectToAction("Index");
}
}
My View Code:
@model IEnumerable<NursingHomes.Models.Hospitals>
@{
ViewBag.Title = "Hospitals Verify";
}
<br />
<br />
@using (Html.BeginForm("GetData", "Home"))
{
<input type="submit" value="Submit" title="Get Data" />
<h2>Nursing Homes</h2>
<table>
<tr>
<th>Code</th>
<th>HAcode</th>
<th>Name</th>
</tr>
@foreach (NursingHomes.Models.Hospitals item in (List<NursingHomes.Models.Hospitals>) ViewData["hosp"])
{
<tr>
<td>
@Html.DisplayFor(modelItem=>item.Code)
</td>
<td>
@Html.DisplayFor(modelItem=>item.HAcode)
</td>
<td>
@Html.DisplayFor(modelItem=>item.Name)
</td>
</tr>
}
</table>
}
I am doing something wrong above? Is is this simply not possible without the use of JQuery?
Basic question is, how can click on that button on the Index page, and tell it to run the GetData action and then build the table below the button using the returned List from the GetData action.