简体   繁体   English

在视图中的字符串内使用剃刀

[英]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. 我试图在链接中放置一些Razor东西,但不能完全正确地注册它。 The part that is going wrong is the @{ @Url... 出问题的部分是@{ @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. 您尝试在Razor块中使用Razor代码。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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