0

我有一个用于编辑课程的页面。在此页面上有一个部分,您可以在该部分中动态添加和删除页面上的类事件。我的问题是在本节中出现的一件事是约会。我只想显示日期部分而不是时间。这是为实体框架设置的类。

public class ClassEvent
{
    public ClassEvent()
    {
        EventDate = DateTime.Today;
    }
    [HiddenInput(DisplayValue = false)]
    public int ID { get; set; }

    public string Description { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date")]
    public DateTime? EventDate { get; set; }

    [HiddenInput(DisplayValue = false)]
    [Required(ErrorMessage = "Please select a class")]
    public int ClassID { get; set; }

    [Display(Name = "Class")]
    [ForeignKey("ClassID")]
    public virtual ScheduledClass ScheduledClass { get; set; }

    public Boolean Delete { get; set; }
}

我将此代码用作日期时间对象的编辑器模板。

@inherits System.Web.Mvc.WebViewPage<System.DateTime?> 

@Html.TextBox("", String.Format("{0:d}", Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") :""), new { @class = "datefield", style="width:100px"})

我在实际的 scheduleclass 类中有另一个字段,如下所示

public class ScheduledClass
{
    public ScheduledClass()
    {
        Attendees = new List<ClassAttendee>();
    }

    [HiddenInput(DisplayValue = false)]
    public int ID { get; set; }

    [HiddenInput(DisplayValue = false)]
    [Required(ErrorMessage = "Please enter a topic")]
    public int ClassTopicID { get; set; }

    [Display(Name = "Topic")]
    public virtual ClassTopic ClassTopic { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int ClassTypeID { get; set; }

    [Display(Name = "Class Type")]
    public virtual ClassType ClassType { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Class Date")] 
    public DateTime ClassDate { get; set; }

    [Display(Name = "Attendees")] 
    public virtual ICollection<ClassAttendee> Attendees { get; set; }

    [Display(Name = "Events")]
    public virtual ICollection<ClassEvent> ClassEvents { get; set; }
}

加载页面时,编辑器模板适用于 ClassDate,但在页面中,如果我单击按钮添加类事件,则编辑器模板不会应用于创建的内容。这是 ClassEvent 本身的编辑器模板

@model TRIOSoftware.Domain.Entities.ClassEvent

<tr class="ClassEvent">
<td>
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.ClassID )
    @Html.TextBoxFor(model => model.Description, new {style ="width:125px"})
    @Html.ValidationMessageFor(model => model.Description)
</td>
<td>    
    @Html.TextBoxFor(model => model.EventDate, new { @class = "datefield", style = "width:150px" })
    @Html.ValidationMessageFor(model => model.EventDate)
</td>
<td>
    @Html.HiddenFor(x => x.Delete, new { @class = "mark-for-delete" })
    @Html.LinkToRemoveNestedForm("Remove", "tr.ClassEvent", "input.mark-for-delete")
</td>
</tr>

有没有人遇到过这个问题?

4

1 回答 1

0

问题最终是我使用 TextBoxFor 并且我需要使用 EditorFor 才能找到我的编辑器模板

于 2013-03-14T16:24:23.277 回答