在页面上创建带有数据库中存储的网址的链接

发布于 2024-08-25 02:27:07 字数 271 浏览 8 评论 0原文

这看起来应该很容易,但我似乎无法弄清楚。我所有的谷歌搜索都会引导我链接到数据库,这不是我想要做的。我是一个完全的网络开发新手。

我大致按照 NerdDinner 教程创建了我的网络应用程序。我存储的字段之一是网址。在“索引”和“详细信息”页面上,当我显示记录中的信息时,我希望网址成为指向该网站的可点击链接。

目前显示为:

<%= Html.Encode(Model.Subcontract.company1.website) %>

This seems like it should be easy, but I can't seem to figure it out. All of my google searches lead me to linking to databases which isn't what I want to do. I'm a complete web development newb.

I've roughly followed the NerdDinner tutorial in creating my web app. One of my stored fields is a web address. On the Index and Details pages, when I display the info from my record, I want the web address to be a clickable link to the website.

It's currently displayed as:

<%= Html.Encode(Model.Subcontract.company1.website) %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鼻尖触碰 2024-09-01 02:27:07

试试这个:

<a href="<%= Html.Encode(Model.Subcontract.company1.website) %>"><%= Html.Encode(Model.Subcontract.company1.website) %></a>

或者

<a href="<%= Html.Encode(Model.Subcontract.company1.website) %>">Company website</a>

如果您使用 DataAnnotations,您可以阅读有关 DataTypeAttribute 的内容。如果您使用 EmailAddress 数据类型来装饰此属性并使用 DisplayFor 帮助程序,您将获得类似的效果。

Try this:

<a href="<%= Html.Encode(Model.Subcontract.company1.website) %>"><%= Html.Encode(Model.Subcontract.company1.website) %></a>

or

<a href="<%= Html.Encode(Model.Subcontract.company1.website) %>">Company website</a>

If you use DataAnnotations, you can read about DataTypeAttribute. If you decorate property with this property with EmailAddress data type and use DisplayFor helper, you'll get similar effect.

当梦初醒 2024-09-01 02:27:07

那么您只想使 Model.Subcontract.company1.website 返回的信息可点击吗?如果是这样,您可以将该信息写入锚标记,如下所示:

<a href="<%= Html.Encode(Model.Subcontract.company1.website) %>"><%= Html.Encode(Model.Subcontract.company1.website) %></a>

否则,您可以在 PageLoad 事件上使用

<asp:HyperLink ID="mylink" runat="server" />

并将以下内容放置在后面代码的 PageLoad 事件中:

mylink.NavigateUrl = Model.Subcontract.company1.website
mylink.Text = Model.Subcontract.company1.website

So you just want to make the information returned by Model.Subcontract.company1.website clickable? If so you can just write that information into an anchor tag like so:

<a href="<%= Html.Encode(Model.Subcontract.company1.website) %>"><%= Html.Encode(Model.Subcontract.company1.website) %></a>

Otherwise you can do it on the PageLoad event by using an

<asp:HyperLink ID="mylink" runat="server" />

and placing the following in the PageLoad event of the code behind:

mylink.NavigateUrl = Model.Subcontract.company1.website
mylink.Text = Model.Subcontract.company1.website
寂寞美少年 2024-09-01 02:27:07

您可以为生成链接的 HTML 帮助器类创建两个扩展方法:

public static MvcHtmlString HtmlLink(this HtmlHelper html, string url, string text)
{
    return HtmlLink(html, url, text, null);
}

public static MvcHtmlString HtmlLink(this HtmlHelper html, string url, string text, object htmlAttributes)
{
    TagBuilder tb = new TagBuilder("a");
    tb.InnerHtml = text;
    tb.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    tb.MergeAttribute("href", url);
    return MvcHtmlString.Create(tb.ToString(TagRenderMode.Normal));
}

然后您可以这样做:

<%= Html.HtmlLink(Model.Subcontract.company1.website, Model.Subcontract.company1.website) %>

You can create two extension methods for the HTML helper class that generates a link:

public static MvcHtmlString HtmlLink(this HtmlHelper html, string url, string text)
{
    return HtmlLink(html, url, text, null);
}

public static MvcHtmlString HtmlLink(this HtmlHelper html, string url, string text, object htmlAttributes)
{
    TagBuilder tb = new TagBuilder("a");
    tb.InnerHtml = text;
    tb.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    tb.MergeAttribute("href", url);
    return MvcHtmlString.Create(tb.ToString(TagRenderMode.Normal));
}

Then you can just do this:

<%= Html.HtmlLink(Model.Subcontract.company1.website, Model.Subcontract.company1.website) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文