我认为这句话如下:
<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>
答案 0 :(得分:1)
您可以使用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>
答案 1 :(得分:0)
我得到了它的工作。我所要做的就是在我的模型中使用这个using子句:
using System.ComponentModel.DataAnnotations;
然后我将这行代码放在定义我的发布日期的代码之上:
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
然后它完美地工作,我没有必要改变视图中的任何内容。