简体   繁体   English

如何使用@HTML.DisplayNameFor 在asp.net mvc 中格式化日期时间变量

[英]how to format a date time variable in asp.net mvc using @HTML.DisplayNameFor

I have this statement in my view that as follows:我的观点如下:

<th>           
    @Html.DisplayNameFor(model => model.ReleaseDate)
</th>

all I'm trying to do is format the release date so that it will be in the format of mm/dd/yyyy.我要做的就是格式化发布日期,使其采用 mm/dd/yyyy 格式。 I've searched loads and haven't come across anything that is exactly what I'm looking for.我搜索了很多东西,但没有遇到任何我正在寻找的东西。 Any help would be greatly appreciated, I'm relatively new to coding.任何帮助将不胜感激,我对编码相对较新。

Here is my Model:这是我的模型:

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace MvcMovie2.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public string Rating { get; set; }
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }

    public class DummyModel : Movie
    {
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime ReleaseDate { get; set; }
    }
}

and here is my view:这是我的观点:

@model IEnumerable<MvcMovie2.Models.Movie>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>           
            @Html.DisplayFor(model => model.ReleaseDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Genre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rating)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReleaseDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

You can achieve that with the DisplayFormatAttribute :您可以使用DisplayFormatAttribute实现这一点:

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace MvcMovie2.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public string Rating { get; set; }
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }
}

Your view is strongly typed to IEnumerable<MvcMovie2.Models.Movie> therefore you cannot use the html helper like you did.您的视图强类型为IEnumerable<MvcMovie2.Models.Movie>因此您不能像以前那样使用 html 帮助程序。 Instead separate your code in partial views that are strongly type to MvcMovie2.Modes.Movie :而是将您的代码在强类型为MvcMovie2.Modes.Movie部分视图中分开:

Code for the actual view :实际视图的代码:

@model IEnumerable<MvcMovie2.Models.Movie>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    @Html.Partial("Header")
    @foreach (var item in Model) 
    {
        @Html.Partial("Movie", item)
    }
</table>

Code for the Header.cshtml partial view : Header.cshtml部分视图的代码:

@model MvcMovie2.Models.Movie

<tr>       
    <th>
        @Html.DisplayNameFor(model => model.Title)
    </th>
    <th>           
        @Html.DisplayNameFor(model => model.ReleaseDate)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Genre)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Rating)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Price)
    </th>
    <th></th>
</tr>

Code for the Movie.cshtml partial view : Movie.cshtml部分视图的代码:

@model MvcMovie2.Models.Movie

<tr>        
    <td>
        @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ReleaseDate)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Genre)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Rating)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Price)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
</tr>

I got it to work.我让它工作。 All I had to do was put this using clause in my model:我所要做的就是把这个 using 子句放在我的模型中:

using System.ComponentModel.DataAnnotations;

then I put this line of code above my code that defined my Release Date:然后我将这行代码放在定义我的发布日期的代码之上:

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]

and then it worked perfectly, I didn't have to change anything in the view.然后它工作得很好,我不需要改变视图中的任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM