Asp.net mvc 3 如何在MVC视图中放置空白和更改日期格式?

Asp.net mvc 3 如何在MVC视图中放置空白和更改日期格式?,asp.net-mvc-3,Asp.net Mvc 3,我正在开发MVC应用程序并使用razor语法 在这个应用程序中,我提供了评论工具 目前的评论看起来像 P Moris9/15/2012 5:40:44 PM Test comment 1 P Moris9/15/2012 5:40:44 PM Test comment 2 P moris9/15/2012 5:40:45 PM Test comment 3 现在,我想在注释所有者的名字和日期时间之间留一个空格。 以及如何将dateTime转换为 dd MMM yy hh:mm tt

我正在开发MVC应用程序并使用razor语法

在这个应用程序中,我提供了评论工具

目前的评论看起来像

P Moris9/15/2012 5:40:44 PM
Test comment 1 


P Moris9/15/2012 5:40:44 PM
Test comment 2 


P moris9/15/2012 5:40:45 PM
Test comment 3
现在,我想在注释所有者的名字和日期时间之间留一个空格。 以及如何将dateTime转换为

dd MMM yy hh:mm tt

评论应该看起来像

p莫里斯2012年9月17日下午5:45

测试意见1

(我不能在上面的示例中提供多个空格…它会自动删除空格。)

我该怎么做

我的代码是

 @foreach (var item in Model)
    {
        <div id="OwnerName">
    
         <span class="EmpName"> @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })</span>
         
           @Html.DisplayFor(ModelItem => item.CommentDateTime)
         
        </div>
                       
     
        
        <p class="CommentP">
           @Html.DisplayFor(ModelItem => item.CommentText)
        </p>
        
        <br />
        

    }
@foreach(模型中的变量项)
{
@ActionLink(item.Owner.FullName,“详细信息”,“员工”,新的{id=item.OwnerId},新的{@style=“color:#1A6690;”)
@DisplayFor(ModelItem=>item.CommentDateTime)

@DisplayFor(ModelItem=>item.CommentText)


}
您可以在评论作者姓名和创建日期之间放置一个

<div id="OwnerName">
    <span class="EmpName">
        @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })
   </span>
   &nbsp;
   @Html.DisplayFor(ModelItem => item.CommentDateTime)
</div>
如果出于某种原因,您没有使用视图模型,并且无法修改实体,则可以在视图中对其进行格式化:

<div id="OwnerName">
    <span class="EmpName">
        @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })
   </span>
   &nbsp;
   @Model.CommentDateTime.ToString("dd MMM yyyy hh:mm tt")
</div>
然后将自定义显示模板名称传递给
DisplayFor
helper:

@Html.DisplayFor(ModelItem => item.CommentDateTime, "CommentDate")

您可以在注释作者姓名和创建日期之间放置一个

<div id="OwnerName">
    <span class="EmpName">
        @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })
   </span>
   &nbsp;
   @Html.DisplayFor(ModelItem => item.CommentDateTime)
</div>
如果出于某种原因,您没有使用视图模型,并且无法修改实体,则可以在视图中对其进行格式化:

<div id="OwnerName">
    <span class="EmpName">
        @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })
   </span>
   &nbsp;
   @Model.CommentDateTime.ToString("dd MMM yyyy hh:mm tt")
</div>
然后将自定义显示模板名称传递给
DisplayFor
helper:

@Html.DisplayFor(ModelItem => item.CommentDateTime, "CommentDate")

对于空格,请使用下面的代码

@{
  @:  &nbsp;&nbsp;
}

对于空格,请使用下面的代码

@{
  @:  &nbsp;&nbsp;
}

谢谢,但是我不能使用@Model.CommentDateTime,我必须使用
@Html.DisplayFor(ModelItem=>item.CommentDateTime)
在这种情况下,您可以使用
[DisplayFormat]
属性来装饰视图模型上的
CommentDateTime
属性,该属性允许您指定所需的格式。另一种可能是编写自定义显示模板。我在应用程序中没有使用视图模型,应该添加它吗?是的,在ASP.NET MVC中使用视图模型是正确的方法。您不应该将域实体传递给视图。我将遵循您建议的最后一个选项…但我对要遵循的STPE有些困惑…意味着我应该只在cshtml类中编写以下代码
@model-DateTime@model.ToString(“dd-MMM-yyy-hh:mm-tt”)
谢谢,但我不能使用@model.CommentDateTime,我必须使用
@Html.DisplayFor(ModelItem=>item.CommentDateTime)
在这种情况下,您可以使用
[DisplayFormat]来装饰视图模型上的
CommentDateTime
属性
属性,可用于指定所需格式。另一种可能是编写自定义显示模板。我在应用程序中没有使用视图模型,应该添加它吗?是的,在ASP.NET MVC中使用视图模型是正确的方法。您不应该将域实体传递给视图。我将遵循您建议的最后一个选项…但我对要遵循的STPE有些困惑…意味着我应该只在cshtml类中编写以下代码<代码>@model DateTime@model.ToString(“dd-MMM-yyy-hh:mm-tt”)