简体   繁体   中英

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);
    }

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 })">

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 = "brahbrahbrah@bbhmm.com";
}

And then render:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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