0

I have helper where want to show month and day separately

I try to do it like this

  @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>

}

But I have error

Severity Code Description Project File Line Suppression State Error CS1501 No overload for method 'ToString' takes 1 arguments 6_AppCode_PostHelper.cshtml C:\Users\Eugene\Source\Repos\vrv2\VrBlog\AppCode\PostHelper.cshtml 9 Active

And here class of Post

 public partial class Post
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Post()
    {
        this.Comments = new HashSet<Comment>();
        this.Tags = new HashSet<Tag>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public Nullable<System.DateTime> DateTime { get; set; }
    public string Body { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Comment> Comments { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Tag> Tags { get; set; }
}

How I can fix it?

Sukhomlin Eugene
  • 183
  • 4
  • 19

1 Answers1

6

You must have nullable datetime. It does not provide method extension for .ToString() having formater you want. Do the following:

<div class="month">@post.DateTime.Value.ToString("MMM").ToUpper()</div>

In below example I have replicated your error: https://dotnetfiddle.net/#&togetherjs=hMm7jkVbbA

Tejas Vaishnav
  • 492
  • 2
  • 14