简体   繁体   English

在将链接传递到视图之前,先在c#MVC 5中将其添加到数据表

[英]Add a Link to a Datatable in c# MVC 5 before passing it to a view

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. 我的问题是,一旦发送到视图,我放在edit或delete列(链接所在的位置)中的任何内容都将以字符串形式出现。

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控件

 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" 在视图中,我只是获取字符串“ 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. 下面的代码是添加HyperLink控件和href字符串的示例。

Does anyone have a solution on how to add a hyperlink or URL into a datatable 'before' sending it to the view. 是否有人对如何在将超链接或URL添加到数据表之前“添加”一个解决方案。

 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>

@Html.Raw might help you. @ Html.Raw可能会帮助您。

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>

I was able to achieve the desired result by using HtmlStrings. 通过使用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);
}

Why don't you just add the hyperlinks on aspx view this way you don't have to create the hyperlinks. 为什么不以这种方式在aspx视图上添加超链接,而不必创建超链接。 just send the id of the row in data table and bind it on view. 只需在数据表中发送该行的ID并将其绑定到视图即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM