Jimmyt1988
Jimmyt1988

Reputation: 21126

Using razor inside a string on view

I am trying to place some Razor stuff inside a link but can't quite get it to register correctly. The part that is going wrong is the @{ @Url...

<a class="social-icons-anchor" HREF="mailto:?subject=Check out this blah! - Blah&body=@{ @Url.AbsoluteAction(" Details", "Blah" , new { blahID = Model.Blah.BlahID }); }">

What's the correct way of placing this kind of link in?

Edit:

The outcome seems to be this (Notice the spacing):

https://blah-blahdev.azurewebsites.net/blah/ details?blahID=121 

Here is the method helper I am using:

public static class UrlHelperExtension
{
    public static string AbsoluteAction(this UrlHelper url, string actionName, string controllerName, object routeValues = null)
    {
        string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
        return url.Action(actionName, controllerName, routeValues, scheme);
    }

Upvotes: 1

Views: 144

Answers (2)

Maximilian Ast
Maximilian Ast

Reputation: 3499

You try to use Razor code inside a Razor block. To fix this, just remove the @{...} :

<a class="social-icons-anchor" HREF="mailto:?subject=Check out this blah! - Blah&body= @Url.AbsoluteAction(" Details", "Blah" , new { blahID = Model.Blah.BlahID })">

Upvotes: 4

Razvan Dumitru
Razvan Dumitru

Reputation: 12452

Basically you must remove the @{}, but.

Create a helper:

@helper MailToThing(string email, string body, string title, string subject) {
    <a class="social-icons-anchor" href="mailto:@email?subject=@subject&body=@body">@title</a>
}

Then you will have to get the blah,blah thing:

@{
 var body = @Url.AbsoluteAction("Details", "Blah" , new { blahID = Model.Blah.BlahID });
 var email = "[email protected]";
}

And then render:

@MailToThing(email, body, "", "Check out this blah! - Blah")

Upvotes: 2

Related Questions