MailTo link in Razor

前端 未结 5 751
心在旅途
心在旅途 2021-01-04 01:14

How do I make a mailto link using Razor?

I\'ve seen Html.MailTo, but when I try @Html.MailTo nothing comes up.

Thanks!

相关标签:
5条回答
  • 2021-01-04 01:20
    @{var email= new HtmlString( "" mail me ""));} @email;
    0 讨论(0)
  • 2021-01-04 01:24

    I get to this question when I was trying to do simple

    <a href="mailto:@ViewBag.EmailTo">Email</a>
    

    but Razor will treat this as text

    I know there are HtmlHelpers and such but so far simple code worked for me

    <a href="mailto:@{@ViewBag.EmailTo}">Email</a>
    

    Whether a variable comes from model or ViewBag it's about @ sign in href and Razor

    0 讨论(0)
  • 2021-01-04 01:30

    HTML.MailTo() helper is a part of the 'mvc3 futures' project, but there is an alternative to way to do it.

    1.)Create a new .cshtml file inside App_Code directory and name it as you want (for example HTMLHelpers.cshtml)

    2.)Write the following in the file

    @helper EmailTextBox(string email, string title) {
        <a href="mailto:@email">@title</a>    
    }
    

    3.)Now in your view you can call your new function. For example write

    Email: @HTMLHelpers.EmailTextBox("george@example.com","George Chatzimanolis")
    
    0 讨论(0)
  • 2021-01-04 01:39

    You should just make a normal hyperlink:

    <a href="mailto:you@example.com">...</a>
    

    Razor and MVC helper methods are not intended to replace HTML tags; they're intended to make common data-bound elements simpler.

    0 讨论(0)
  • 2021-01-04 01:40

    The mailto helper is a part of the 'mvc3 futures' project.

    The below blog will give you more information on mvc3 futures as well as the link to get it. I believe that it is also available as a NuGet package.

    http://weblogs.asp.net/imranbaloch/archive/2011/07/26/using-the-features-of-asp-net-mvc-3-futures.aspx

    0 讨论(0)
提交回复
热议问题