方法'ToString'没有重载需要1个参数(ASP.NET MVC)

时间:2017-07-10 13:03:12

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我有帮助,想要分别显示月和日

我尝试这样做

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

}

但我有错误

  

严重级代码描述项目文件行抑制状态   错误CS1501方法'ToString'没有重载需要1个参数6_AppCode_PostHelper.cshtml C:\ Users \ Eugene \ Source \ Repos \ vrv2 \ VrBlog \ AppCode \ PostHelper.cshtml 9活动

这里的帖子

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

我该如何解决?

1 个答案:

答案 0 :(得分:6)

您必须拥有可为空的日期时间。它没有为.ToString()提供你想要的格式化程序的方法扩展。 执行以下操作:

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

在下面的示例中,我复制了您的错误: https://dotnetfiddle.net/#&togetherjs=hMm7jkVbbA