Reputation: 113
Today i need to add a link to one of my tables columns.
The table is created as a datatable in my controller, in which it is populated by a database query and then sent to the view. There are quite a few solutions to put a link in a datatable but so far i can't seem to find a solution that doesn't do this in the view.
My problem here is that once sent to the view anything i put in the edit or delete columns (This is where the links are) just comes out as a string.
If try to insert this into the table
<a href=\"delete?Id=\"" + reader.GetValue(0) + "\">delete</a>"
then on the view i get the string
"<a href=\"delete?Id=\"" + reader.GetValue(0) + "\">delete</a>".
if try creating a HyperLink control like this
HyperLink delete = new HyperLink() { Text = "delete", NavigateUrl = "/delete?Id =\"" + reader.GetValue(0) };
and then add the control to the table. In the view i simply get the string "System.Web.UI.WebControls.HyperLink"
I'm a little lost on how to proceed from here. Below is my full controller method that populates the datatable and passes it to the view. The code below is an example adding both a HyperLink control and an href string.
Does anyone have a solution on how to add a hyperlink or URL into a datatable 'before' sending it to the view.
public ActionResult Moved()
{
DataTable table = new DataTable();
table.Columns.Add("Moved From", typeof(string));
table.Columns.Add("Moved To", typeof(string));
table.Columns.Add("Comment", typeof(string));
table.Columns.Add("Edit");
table.Columns.Add("Delete");
using (var reader = SqlHelper.ExecuteReader(ConfigurationManager.ConnectionStrings["RedirectsReader"].ConnectionString, "usp_Redirect_SelectByType", new SqlParameter("@type", 2), new SqlParameter("@sort", "pattern")))
{
while (reader.Read())
{
HyperLink edit = new HyperLink() { Text = "edit", NavigateUrl = "/edit?Id =\"" + reader.GetValue(0) };
HyperLink delete = new HyperLink() { Text = "delete", NavigateUrl = "/delete?Id =\"" + reader.GetValue(0) };
table.Rows.Add(reader.GetValue(1).ToString(), reader.GetValue(2).ToString(), reader.GetValue(3).ToString(),edit,"<a href=\"delete?Id=\"" + reader.GetValue(0) + "\">delete</a>");
}
}
return View(table);
}
This is how my tables are created in the view
<table class="table table-striped table-bordered display" cellspacing="0" width="100%" id="contentTable">
<thead>
<tr>
@foreach (DataColumn col in Model.Columns)
{
<th>@col.ColumnName</th>
}
</tr>
</thead>
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
@foreach (DataColumn col in Model.Columns)
{
<td>@row[col.ColumnName]</td>
}
</tr>
}
</tbody>
</table>
Upvotes: 0
Views: 3800
Reputation: 113
I was able to achieve the desired result by using HtmlStrings.
Below is an example in case anyone else tries to do the same.
Public ActionResult TestPage()
{
DataTable table = new DataTable();
table.Columns.Add("Link Column", typeof(HtmlString));
HtmlString link = new HtmlString("<a href= **Your Link Here** >Click Here</a>")
Table.Rows.Add(link)
return View(table);
}
Upvotes: 1
Reputation: 395
@Html.Raw might help you.
while creating links remove escape char as they might get you some error.
table.Rows.Add( "edit", "<a href=delete?Id=" +1 + ">delete</a>");
so now you have to change your table row to look like this
<td>@Html.Raw( @row[col.ColumnName])</td>
Upvotes: 2
Reputation: 395
Why don't you just add the hyperlinks on aspx view this way you don't have to create the hyperlinks. just send the id of the row in data table and bind it on view.
Upvotes: 0