I have a datatable, on that datatable i set a Html.ActionLink. When I click that action link, I want to send an id of the item to a javascript function and have a new datatable appear below with all of its content that belongs to the selected item in the datatable above. So for example if I click a students name in a table, I want all the students Grades and Test to appear below in a separate datatable. I’ve never worked with javascript much so I’m not sure how I can do this. If someone can please point me in the right direction or give some tips I’d appreciate it.
original first datatable:
@foreach (var item in ((List<Epic>) ViewData["selectedestimate"]))
{
<tr>
<td>
@* @Html.ActionLink(@item.Name, "action", "controller", new {id = item})*@
<a href="#" onclick="StoryClick(@item.Id);">@item.Name</a>
</td>
Javascript to call:
<script type="text/javascript">
function StoryClick(story) {
$.get("@Url.Action("action", "controller")", function (response) {
$('#stories').accordion({ collapsible: true });
});
}
</script>
ActionController:
public List<EpicDetails> getEpicDetails(int id)
{
return eRepository.getItemsById(id).tolist();
}
Or do I need an ActionResult?
public Actionresult Details(int id)
{
}
I realize that I’m not even close right now, but its just b/c I’m not sure what steps to take to do this.
Eventually I would make a accordion and put the table in the accordion.
In situations like this I like to actually keep the
<a>theActionLinkgenerates, and just add JavaScript to enhance the behavior of the link. So your view wouldn’t really change (I did add a class so that we can bind an event handler to it later):Then write some jQuery (it looks like you already have a dependency on jQuery. If not, I can revise the answer to use vanilla JavaScript) to bind an event handler to links with class
item-link:And, yes, your action method in the controller should return an
ActionResult. It’s hard for me to say what type ofActionResultyou should return without actually knowing what type of data you want to consume on the client, but if you wanted to inject HTML onto the page, you could write something like this:Then in your JavaScript you would write:
Where
element_to_populatewould be a selector that points to where you want to inject the HTML.