0

I try to create a helper in my project. This is my code:

@using VrBlog.Models;
@helper Render(Post post, System.Web.Mvc.HtmlHelper html, bool isAdmin, bool showComments)
{
    <div class="postTitle"><a href="@Href("~/Views/Posts/Details/" + post.Id)">@post.Title</a></div>
    <div class="postContainer">
        <div class="postTabs">
            <div class="dateTab">
                <div class="month">@post.DateTime.Value.ToString("MMM").ToUpper()</div>
                <div class="day">@post.DateTime.Value.ToString("dd")</div>
            </div>
            <div class="commentsTab">
                <a href="@Href("~/Views/Posts/Details/"+post.Id + "#comments")">@post.Comments.Count</a>
            </div>
        </div>
        <div class="postContent">
            <div class ="postBody">@html.Raw(post.Body)</div>
            <div class="tagList">
                @foreach (Tag tag in post.Tags)
                {
                    <span class="tag"><a href="@Href("~/Views/Posts/Tags" + tag.Name)">@tag.Name</a></span>
                }
            </div>
            <div class="linkList">
                @{ string url = "http://www.mattblagden.com/posts/details/" + post.Id;}
            </div>
        </div>
    </div>

    if (showComments)
    {
        <div id="commentsContainer">
            <a id ="comments"></a>
            @foreach (Comment comment in post.Comments.OrderBy(x => x.DateTime))
            {
                <div class="comment">
                    <div class="commentName">
                        @if (!string.IsNullOrWhiteSpace(comment.Email))
                        {
                            <a href="mailto:@comment.Email">@comment.Name</a>
                        }
                        else
                        {
                            @comment.Name;
                        }
                    </div>
                    said:
                    <div class="commentBody">@html.Raw(html.Encode(comment.Body).Replace("\n", "<br/>"))</div>
                    <div class="commentTime">at @comment.DateTime.Value.ToString("HH:mm") on @comment.DateTime.Value.ToString("yyyy/MM/dd")</div>
                </div>
            }
            <div id="commentEditor">
                <div id="commentPromt">Leave a comment!</div>
                <form action="@Href("~/Views/Posts/Comment/" + post.Id)" method="post">
                    <input type="text" id="commentNamePromt" name="name"/>Name (required)<br/>
                    <input type="text" id="commentEmailPromt" name="email" />Email (optional)<br/>
                    <textarea id="commentBodyInput" name="body" rows="10" cols="60"></textarea><br/>
                    <input type="submit" id="commentSubmitInput" name="submit" value="Submit!"/>
                </form>
            </div>
        </div>
    }
}

And after this, I try to call it from the view. My helper is in the AppCode folder inside the project.

I try to call it like this

@foreach (Post post in Model)
{
    @PostHelper.Render(post,Html,isAdmin, false)
}

But I get the following error:

Error CS0103 The name 'PostHelper' does not exist in the current context

How I can fix this and why it not visible?

Tejas Vaishnav
  • 492
  • 2
  • 14
Sukhomlin Eugene
  • 183
  • 4
  • 19

3 Answers3

0

Change this

@foreach (Post post in Model)
{
    @PostHelper.Render(post,Html,isAdmin, false)
}

to

@foreach (Post post in Model)
{
    @Render(post,Html,isAdmin, false)
}
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
  • Cannot resolve Render. Look. My helper is on App_Code folder in project. I try to rebuilt project it not helps. Seems VS not see it. – Sukhomlin Eugene Jul 10 '17 at 16:16
0

If you are calling the helper on the same page, try following:

@foreach (Post post in Model)
{
    @Render(post, Html, isAdmin, false)
}
Tejas Vaishnav
  • 492
  • 2
  • 14
0

Rename your folder in App_Code

"We can define the @helper method outside of our view template, and enable it to be re-used across all of the view templates in our project.

We can accomplish this by saving our @helper methods within .cshtml/.vbhtml files that are placed within a \App_Code directory that you create at the root of a project."

Ban Istvan
  • 101
  • 8