Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何让视图显示数据库表_Asp.net Mvc_Asp.net Mvc 4_Asp.net Core - Fatal编程技术网

Asp.net mvc 如何让视图显示数据库表

Asp.net mvc 如何让视图显示数据库表,asp.net-mvc,asp.net-mvc-4,asp.net-core,Asp.net Mvc,Asp.net Mvc 4,Asp.net Core,我是ASP.net新手。我正试图找出如何使我的编辑/显示页面在多选列表框中正常工作 我的create工作正常,并保存到数据库中,但我不知道如何返回到编辑页面,仍然可以看到所选的值。 希望这是有意义的 下面是我为create方法编写的代码。该记录在两个表中都保存得很好,但从选项表中获取值时遇到问题。 我想尝试使“编辑”视图看起来像“创建”视图 控制器 [HttpPost] public IActionResult Create(MusicViewModel model) { if(Mode

我是ASP.net新手。我正试图找出如何使我的编辑/显示页面在多选列表框中正常工作

我的create工作正常,并保存到数据库中,但我不知道如何返回到编辑页面,仍然可以看到所选的值。 希望这是有意义的

下面是我为create方法编写的代码。该记录在两个表中都保存得很好,但从选项表中获取值时遇到问题。 我想尝试使“编辑”视图看起来像“创建”视图

控制器

[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();
}
音乐中心

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; }
}
公共枚举类型
{
Spotify,
沟槽
光盘
乙烯基,
潘多拉
}
公共音乐课
{
公共int MusicID{get;set;}
[必需]
[最大长度(50),最小长度(5)]
公共字符串艺术家{get;set;}
[必需,最小长度(5)]
公共字符串相册{get;set;}
公共整数评级{get;set;}
公共标签{get;set;}
[数据类型(DataType.Date)]
[显示(Name=“发布日期”)]
公共日期时间释放日期{get;set;}
公共字符串审阅{get;set;}
公共列表选项{get;set;}
}
公共类选项
{
公共int选项ID{get;set;}
公共类型类型{get;set;}
公共int MusicID{get;set;}
公共音乐{get;set;}
}
公共类MusicDbContext:DbContext
{
公共数据库集相册{get;set;}
公共数据库集选项{get;set;}
}
看法

@模型音乐
....
@Html.DropDownList(“Label”,Html.GetEnumSelectList(typeof(Label)),“----”,new{@class=“form control”})

我想出来了,可能不是最有效的方法,但至少代码是有效的

[HttpPost]
    public IActionResult Edit(int id,MusicViewModel model)
    {
        var album = _musicData.GetM(id);

        if (album != null && ModelState.IsValid)
        {
            album.Album = model.Album;
            album.Artist = model.Artist;
            album.Label = model.Label;
            album.Review = model.Review;
            album.ReleaseDate = model.ReleaseDate;
            _musicData.RemoveOptions(id);

            foreach (Types type in model.Options)
            {
                var opt = new Options();
                opt.MusicID = id;
                opt.Types = type;
                _musicData.AddOptions(opt);

            }

                _musicData.Commit();
            return RedirectToAction("Details",id);
        }
        return View(album);
    }

我发现这可能不是最有效的方法,但至少代码是有效的

[HttpPost]
    public IActionResult Edit(int id,MusicViewModel model)
    {
        var album = _musicData.GetM(id);

        if (album != null && ModelState.IsValid)
        {
            album.Album = model.Album;
            album.Artist = model.Artist;
            album.Label = model.Label;
            album.Review = model.Review;
            album.ReleaseDate = model.ReleaseDate;
            _musicData.RemoveOptions(id);

            foreach (Types type in model.Options)
            {
                var opt = new Options();
                opt.MusicID = id;
                opt.Types = type;
                _musicData.AddOptions(opt);

            }

                _musicData.Commit();
            return RedirectToAction("Details",id);
        }
        return View(album);
    }

不能将
元素绑定到复杂对象的集合(这就是
列表选项
的含义)。您需要一个具有属性
IEnumerable
的视图模型来将所选值绑定到。这是用于MVC4还是ASP.NET Core?你的标签相互矛盾。询问问题时请使用正确的标记。您无法将
元素绑定到复杂对象的集合(这就是
列表选项
的含义)。您需要一个具有属性
IEnumerable
的视图模型来将所选值绑定到。这是用于MVC4还是ASP.NET Core?你的标签相互矛盾。提问时请使用正确的标签