Search code examples
c#asp.net-mvchelper

MVC C# List Email with a mailto: {variable} from the database


I have a view page where I am listing a username and their email address from the database and I was wondering how to display the email so when it is clicked on it opens up the outlook mailbox and sends and email to that address:

Here is my view:

@model IEnumerable<Comtrex_ICU.Models.UserProfile>


@{
    ViewBag.Title = "UserTable";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2 class="admin-home-link orange-titles">@Html.ActionLink("User Information", "AdminIndex")</h2>
<p>&nbsp;</p>

@foreach (var item in Model)
{

        <p>@Html.DisplayFor(modelItem => item.UserName) | @Html.ActionLink("<a href=mailto:"(modelItem => item.Email)">(modelItem => item.Email)</a>")</p>


}

Anyone know the proper syntax?


Solution

  • you're passing your <a> tag into Html.ActionLink as a parameter which is not something that it expects to receive. Instead, you can omit the Html.ActionLink entirely and just build the <a> tag as follows:

    <p>@Html.DisplayFor(modelItem => item.UserName) | <a href="@string.Format("mailto:{0}", item.Email)">@item.Email</a></p>
    

    note that Html.ActionLink will generate the entire <a> tag for you and is used when you'd like to generate a link that will hit an action on one of your controllers so that doesn't really fit with what you're trying to do and is why it isn't needed.