title: Add a Link to a Datatable in c# MVC 5 before passing it to a view
body: <p>Today i need to add a link to one of my tables columns.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>If try to insert this into the table </p>
  
  <pre><code>&lt;a href=\"delete?Id=\"" + reader.GetValue(0) + "\"&gt;delete&lt;/a&gt;"
  </code></pre>
  
  <p>then on the view i get the string </p>
  
  <pre><code>"&lt;a href=\"delete?Id=\"" + reader.GetValue(0) + "\"&gt;delete&lt;/a&gt;".
  </code></pre>
  
  <p>if try creating a HyperLink control like this </p>
  
  <pre><code> HyperLink delete = new HyperLink() { Text = "delete", NavigateUrl = "/delete?Id =\"" + reader.GetValue(0) };
  </code></pre>
  
  <p>and then add the control to the table. In the view i simply get the string "System.Web.UI.WebControls.HyperLink"</p>
  
  <p>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.</p>
  
  <p>Does anyone have a solution on how to add a hyperlink or URL into a datatable 'before' sending it to the view.</p>
  
  <pre><code> 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,"&lt;a href=\"delete?Id=\"" + reader.GetValue(0) + "\"&gt;delete&lt;/a&gt;");
              }
          }
          return View(table);
      }
  </code></pre>
  
  <p>This is how my tables are created in the view</p>
  
  <pre><code>&lt;table class="table table-striped table-bordered display" cellspacing="0" width="100%" id="contentTable"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  @foreach (DataColumn col in Model.Columns)
                  {
                      &lt;th&gt;@col.ColumnName&lt;/th&gt;
                  }
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              @foreach (DataRow row in Model.Rows)
              {
                  &lt;tr&gt;
                      @foreach (DataColumn col in Model.Columns)
                      {
                          &lt;td&gt;@row[col.ColumnName]&lt;/td&gt;
                      }
                  &lt;/tr&gt;
              }
          &lt;/tbody&gt;
      &lt;/table&gt;
  </code></pre>