ASP.Net MVC Razor从DateTime属性中删除时间 [英] ASP.Net MVC Razor Remove Time From DateTime Property

查看:189
本文介绍了ASP.Net MVC Razor从DateTime属性中删除时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Razor Views来开发一个ASP.Net MVC 3 Web应用程序。我有以下ViewModel,它被传递到我的Razor View并重复显示记录列表。



ViewModel

  public class ViewModelLocumEmpList 
{
public IList< FormEmployment> LocumEmploymentList {get; set;}
}

查看

 < table> 
< tr>
< th>雇主< / th>
< th> Date< / th>
< / tr>
@foreach(Model.LocumEmploymentList中的var item){
< tr>
< td> @ item.employerName< / td>
< td> @ item.startDate< / td>
< / tr>
}
< / table>

我的问题是行

  @ Html.DisplayFor(modelItem => item.startDate)

返回像这样的日期 20/06/2012 00:00:00 ,我希望删除时间,只显示日期,即 20/06/2012



我已尝试添加

  @Html。 DisplayFor(modelItem => item.startDate.Value.ToShortDateString())

And

  DisplayFor(modelItem => item.startDate.HasValue?item.startDate.Value.ToShortDateString():)

但是,他们都在运行时返回以下错误消息

 模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。 

我看过Darin Dimitrov的回答这里使用剃刀转换DateTime格式



但是,我没有访问startDate属性在我的ViewModel中,我的ViewModel返回一个可以在上面看到的FormEmployment对象的IList。



如果任何人有任何想法,如何从日期时间属性中删除时间那么我将非常感激。



谢谢。

另外,我的startDate属性是Nullable。 >

更新



根据PinnyM的答案,我添加了一个部分类(见下文)将[DisplayFormat]属性放在startDate属性上。

  public partial class FormEmployment 
{
[DisplayFormat (DataFormatString ={0:dd / MM / yyyy})]
public Nullable< System.DateTime> startDate {get;组; }
}

但是,我的Razor View仍然显示 20/06/2012 00:00:00 使用以下代码

  @ Html.DisplayFor(modelItem => item.startDate) 

任何想法?



谢谢。 / p>

解决方案

您可以使用简单的@ item.startDate.Value.ToShortDateString()(为null值添加正确的验证)


I am developing an ASP.Net MVC 3 Web application using Razor Views. I have the following ViewModel which is passed to my Razor View and iterated through to display a list of records.

ViewModel

public class ViewModelLocumEmpList
{
    public IList<FormEmployment> LocumEmploymentList {get; set;}
}

View

<table>
  <tr>
   <th>Employer</th>
   <th>Date</th>
   </tr>
    @foreach (var item in Model.LocumEmploymentList) {
      <tr>
        <td>@item.employerName</td>
        <td>@item.startDate</td>
      </tr>
      }
      </table>

My problem is that the line

@Html.DisplayFor(modelItem => item.startDate)

Returns a date like this 20/06/2012 00:00:00, and I would like it to remove the time and just display the date, ie, 20/06/2012.

I have tried adding

@Html.DisplayFor(modelItem => item.startDate.Value.ToShortDateString())

And

DisplayFor(modelItem => item.startDate.HasValue ? item.startDate.Value.ToShortDateString(): "")

However, they both return the following error message at runtime

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I have looked at Darin Dimitrov’s answer here Converting DateTime format using razor

However, I don’t have access to the startDate property in my ViewModel, my ViewModel returns an IList of FormEmployment objects which you can see above.

If anyone has any idea’s on how to remove the time from the date time property then I would be greatly appreciative.

Thanks.

Also, my startDate property is Nullable.

Update

Based on PinnyM's answer, I added a partial class (see below) to place the [DisplayFormat] attribute on the startDate property.

public partial class FormEmployment
{
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> startDate { get; set; }
}

However, my Razor View still displays 20/06/2012 00:00:00 using the following code

@Html.DisplayFor(modelItem => item.startDate)

Any idea's?

Thanks.

解决方案

You can use simply @item.startDate.Value.ToShortDateString() (adding the proper validation for null value)

这篇关于ASP.Net MVC Razor从DateTime属性中删除时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆