ASP.NET MVC 4将类列表添加到类中

时间:2013-11-14 18:41:37

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在建立一个我去过的音乐会数据库和播放的歌曲。我有2个班,一个Show班和一个Song班。 Show类有一个List。我想知道如何在应用程序前端的节目中添加歌曲。现在我正在使用Microsoft提供的默认CRUD。它现在是基本格式。我已经添加了下面的类定义以及Show类的Create.cshtml:

public class Show
{
    public int ID { get; set; }

    [Required]
    public String Venue { get; set; }

    [Required]
    public String City { get; set; }

    [Required]
    public String State { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime ShowDate { get; set; }

    public List<Song> TrackList { get; set; }
}

public class Song
{
    public int ID { get; set; }

    [Required]
    public int ShowID { get; set; }

    [Required]
    public String Title { get; set; }

    public TimeSpan Length { get; set; }
    public String Notes { get; set; }
}

Create.cshtml

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Show</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Venue)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Venue)
        @Html.ValidationMessageFor(model => model.Venue)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.City)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.City)
        @Html.ValidationMessageFor(model => model.City)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.State)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.State)
        @Html.ValidationMessageFor(model => model.State)
    </div>

    <div class="editor-label">
        @Html.Label("Date")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ShowDate)
        @Html.ValidationMessageFor(model => model.ShowDate)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

0 个答案:

没有答案