2016-06-13 23 views
1

Я новичок в ASP.net. Я пытаюсь выяснить, как заставить страницы редактирования/отображения работать правильно для многосетевого списка.Как получить мое представление, чтобы показать таблицу моей базы данных

Мое создание прекрасно работает и сохраняется в моей базе данных, но я не могу понять, как вернуться на страницу редактирования и по-прежнему видеть выбранные значения. Надеется, что это имеет смысл.

Вот код, который у меня есть для метода create. Запись сохраняется в обеих таблицах, но мне трудно получить значения из моей таблицы параметров. Я хочу, чтобы попытаться сделать вид Edit выглядеть Create View

контроллер

[HttpPost] 
public IActionResult Create(MusicViewModel model) 
{ 
    if(ModelState.IsValid) 
    { 
     var album = new Music(); 
     album.Album = model.Album; 
     album.Artist = model.Artist; 
     album.Label = model.Label; 
     album.Review = model.Review; 
     album.ReleaseDate = model.ReleaseDate; 
     foreach(Types type in model.Options) 
     {var opt = new Options(); 
      opt.Music = album; 
      opt.Types = type; 
      _musicData.AddOptions(opt); 
     } 
     _musicData.Add(album); 
     _musicData.Commit(); 
     return RedirectToAction("Details", new { id = album.MusicID }); 
    } 
    return View(); 
} 

Music.cs

public enum Types 
{ 
    Spotify, 
    Groove, 
    CD, 
    Vinyl, 
    Pandora 
} 

public class Music 
{ 
    public int MusicID { get; set; } 
    [Required] 
    [MaxLength(50),MinLength(5)] 
    public string Artist { get; set; } 
    [Required, MinLength(5)] 
    public string Album { get; set; } 
    public int Rating { get; set; } 
    public Label Label { get; set; } 
    [DataType(DataType.Date)] 
    [Display(Name ="Release Date")] 
    public DateTime ReleaseDate { get; set; } 
    public string Review { get; set; } 
    public List<Options> Options { get; set; } 
} 

public class Options 
{ 
    public int OptionsID { get; set; } 
    public Types Types { get; set; } 
    public int MusicID { get; set; } 
    public Music Music { get; set; } 
} 

public class MusicDbContext:DbContext 
{ 
    public DbSet<Music> Albums { get; set; } 
    public DbSet<Options> Options { get; set; } 
} 

Посмотреть

@model Music 
.... 
<form asp-action="Create" method="post"> 
<div class="row"> 
    <div class="col-md-3 col-md-offset-2"> 
     <fieldset class="form-group"> 
      <label asp-for="Artist"></label> 
      <input class="form-control" asp-for="Artist" /> 
      <span asp-validation-for="Artist" class="alert"></span> 
     </fieldset> 
    </div> 

    <div class="col-md-3"> 
     <fieldset class="form-group"> 
      <label asp-for="Album"></label> 
      <input class="form-control" asp-for="Album" /> 
      <span asp-validation-for="Album" class="alert"></span> 
     </fieldset> 
    </div> 

    <div class="col-md-3"> 
     <label asp-for="Label"></label> 
     @Html.DropDownList("Label", Html.GetEnumSelectList(typeof(Label)), "-------", new { @class = "form-control" }) 
    </div> 
</div> 

<div class="row"> 
    <div class="col-md-3 col-md-offset-2"> 
     <fieldset class="form-group"> 
      <label asp-for="Options"></label> 
      <select multiple class="form-control" asp-for="Options" 
       asp-items="@Html.GetEnumSelectList(typeof(Types))"></select> 
     </fieldset> 
    </div> 
    <div class="col-md-3"> 
     <fieldset class="form-group"> 
      <label asp-for="ReleaseDate"></label> 
      <input type="text" asp-for="ReleaseDate" class="DateBox form-control" /> 
      <span asp-validation-for="ReleaseDate" class="alert"></span> 
     </fieldset> 
    </div> 
    </div> 
    <div class="col-md-offset-3"><input class="btn btn-info" type="submit" value="Submit" /></div> 
</form> 
+0

Вы не можете привязать элемент '