繁体   English   中英

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

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

我的观点如下:

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

我要做的就是格式化发布日期,使其采用 mm/dd/yyyy 格式。 我搜索了很多东西,但没有遇到任何我正在寻找的东西。 任何帮助将不胜感激,我对编码相对较新。

这是我的模型:

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

这是我的观点:

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

您可以使用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; }
    }
}

您的视图强类型为IEnumerable<MvcMovie2.Models.Movie>因此您不能像以前那样使用 html 帮助程序。 而是将您的代码在强类型为MvcMovie2.Modes.Movie部分视图中分开:

实际视图的代码:

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

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>

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>

我让它工作。 我所要做的就是把这个 using 子句放在我的模型中:

using System.ComponentModel.DataAnnotations;

然后我将这行代码放在定义我的发布日期的代码之上:

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

然后它工作得很好,我不需要改变视图中的任何东西。

暂无
暂无

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

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