I have an MVC table that I've created from database query results. The issue that I'm having is that I am trying to create links out of the first column (device/circuit) results that will pass to an .aspx page to display the load history for the circuit that was clicked. I have the links creating and passing to the .aspx page, but the data being displayed is always for the last item.CUIRCUIT returned from the query. Is there any way to retrieve the LinkText from the link that was clicked? Everything that I've seen so far uses static ActionLinks, not variable ones.
As mentioned above, I have tried using the variable item.CIRCUIT LinkText value, and have set a Global Variable to this value to pre-populate a dropdownlist on the .aspx page, but of course I only receive the last Circuit from the list that was returned; not the value of the link that was clicked.
MVC Index code
@model IEnumerable<Mvc.Models.Circuits_View>
@using Mvc.Variables;
…
<div style="max-height:335px;overflow:auto;">
<table class="table table-bordered table-condensed table-striped">
@foreach (var item in Model)
{
<tr>
<td>
@*@Html.DisplayFor(modelItem => item.CIRCUIT)*@
@Html.ActionLink(item.CIRCUIT, null, "AmpLoadDaily", Globals.ddl_cktval1 = item.CIRCUIT, null)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.IA)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.IB)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.IC)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.IG)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.KW)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.KVA)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.KVAR)
</td>
<td align="right">
@Html.DisplayFor(modelItem => item.PF)
</td>
<td>
@Html.DisplayFor(modelItem => item.STATUS)
</td>
</tr>
}
</table>
</div>
ASPX DropDownList
<asp:DropDownList runat="server" ID="ddlDevice1" DataSourceID ="SqlDataSource_ddlDevice1" DataTextField="CIRCUIT" DataValueField="CIRCUIT"
AppendDataBoundItems="true" AutoPostBack="true" Width="95" OnSelectedIndexChanged="ddlDevice1_SelectedIndexChanged">
<asp:ListItem Text="-Device1-" Value=" " Selected="True">
</asp:ListItem>
</asp:DropDownList>
CodeBehind
if (Globals.ddl_cktval1 != "")
{
ddlDevice1.SelectedValue = Globals.ddl_cktval1;
Globals.ddl_cktval1 = "";
}
I expect the value to be the name of the link clicked, (i.e. Device1, or Device23), not always DeviceLast, (unless that was the link clicked, of course).
It seems that I was able to use an Anonymous Type after all to collect the item.CIRCUIT information for the specific ActionLink desired.
@foreach (var item in Model)
{
<tr>
<td>
@*@Html.DisplayFor(modelItem => item.CIRCUIT)*@
@Html.ActionLink(item.CIRCUIT, null, "AmpLoadDaily", new { device = item.CIRCUIT }, null)
</td>
My issue must have come from getting the Request.QueryString correct in the code behind.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlDevice1.SelectedValue = Request.QueryString["device"];
I am now able to pre-populate the DropDownList on the ASPX page with the value provided from the MVC page. I just need to figure out how to have the SqlDataSource's, (that use the selected value as a parameter), re-runat="server" during the page load, after I set the SelectedValue of the DropDownList to the new value passed. The search continues …