I'm having a bit of trouble learning MVC 4 at the moment, and I was hoping someone could help. As far as I can tell, there isn't a duplicate of this question that showed up in search, but if there is, please direct me to it.
My question is this: in a list of schools that I have shown on my index.aspx page, is it possible to then have a user click on the "details" link such that it goes back and brings up the details for that specific school? I've included all of the relevant information from my code below, but please excuse me if it is too much (I wasn't sure exactly what was needed).
As you can see, the ActionResult Index() brings back a list of all schools in my database. What I want to do is post back when they click "details" so that the view then returns with specific information about that school. Is this possible? I figured that it would be possible to just pass the Id of the school that was clicked on, but then when I get back down to the DAC layer, it's accepting an Id (int) as a parameter, when I want it to return an object of type "School". Does this make sense?
For what it's worth, I know a small bit of Javascript, but no jQuery, JSON, etc (yet).
So I have 5 layers:
- Index.aspx (View)
- SchoolController (Controller)
- SchoolBus (Business layer)
- SchoolDAC (Database Access Class)
- SchoolVO (View Object)
My view object:
SchoolVO.cs:
public int Id { get; set; }
public string Name { get; set; }
public string Slogan { get; set; }
My database access class:
SchoolDAC.cs
public static School GetSchool(School school)
{
try
{
using (SqlConnection conn = ConnectionHelper.GetConnection("SchoolDB"))
{
SqlCommand cmd = new SqlCommand("Schools.GetSchool", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SchoolId", school.Id);
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
school = readRecord(dr);
// readRecord() is just another method that fills the 3 properties of the school object with data from the database.
}
}
return school;
}
}
catch (Exception ex)
{
throw new Exception("Failed to get school", ex);
}
}
My "business" layer:
SchoolBus.cs:
public static School GetSchool(School school)
{
school = SchoolDAC.GetSchool(school);
return school;
}
My Controller:
SchoolController.cs:
public ActionResult Index()
{
List<School> model = SchoolBus.GetAllSchools();
return View(model);
}
[HttpPost]
public ActionResult Index(School school)
{
School model = SchoolBus.GetSchool(school);
return View(model);
}
My View:
index.aspx:
<table>
<tr>
<th>
<%: Html.DisplayNameFor(model => model.Id) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Name) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Slogan) %>
</th>
<th></th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.Id) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Slogan) %>
</td>
<td> <!--- possible to pass an object through here? --->
<%: Html.ActionLink("Sign Up", "Index", new { id=item.Id }) %> |
<%: Html.ActionLink("Details", "Details", new { id=item.Id }) %> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.Id }) %>
</td>
</tr>
<% } %>
</table>