ASP.NET MVC ViewModels将多对多模型关系组合到View

时间:2018-04-24 22:50:32

标签: c# asp.net-mvc many-to-many asp.net-mvc-viewmodel

我正在构建经典电影目录项目。

我有多对多的电影和流派,以便我可以在电影中列出类型,反之亦然等。

我在使用我的模型时遇到了麻烦。我可以使用模型将一个与视图相关联,但需要ViewBag来显示另一个。

我是初学者,任何征服高级ViewModel的帮助都将不胜感激。

我使用我的create()电影方法作为例子:

我的MovieController:

[Authorize(Roles = "Admin")]
[HttpGet]
 public ActionResult Create()
        {
            ViewBag.Genres = db.Genres.Select(
                g => new GenreToMovie
                {
                    id = g.Id,
                    Name = g.Name,
                    Checked = false
                }).ToList();
            return View();
        }

...

 //List Genre Movies
        private void MovieLister(Genre genre)
        {
            //Assign Movies to Genre List
            ViewBag.Movies = db.Movies.Select(
                m => new MovieToGenre
                {
                    id = m.Id,
                    Title = m.Title,
                    Year = m.Year,
                    Checked = db.Genres.FirstOrDefault(g => g.Id == genre.Id).Movies.Any(movie => movie.Id == m.Id)
                }).ToList();

我的电影模型:

namespace MovieSite.Models
{
    public class Movie
    {
        public int Id { get; set; }

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

        [Required]
        public int Year { get; set; }
        public virtual ICollection<Genre> Genres { get; set; }

        public Movie() { }

        public Movie(string title, int year)
        {
            Title = title;
            Year = year;
            Genres = new List<Genre>();
        }

    }

}

我的MovieViewModels(一个不好的名字,我敢肯定,它保存了我的GenreTomMovie ViewModel:

namespace MovieSite.Models
{
    //Assign Genres to Movies
    public class GenreToMovie
    {
        public int id { get; set; }
        public string Name { get; set; }
        public bool Checked { get; set; }
    }

    //Assign Movies to Genres
    public class MovieToGenre
    {
        public int id { get; set; }
        public string Title { get; set; }
        public int Year { get; set; }
        public bool Checked { get; set; }
    }

    public class MovieViewModel
    {
        public int MovieID;
        public string Title { get; set; }
        public int Year { get; set; }

        public MovieViewModel() { }

        public MovieViewModel(int id, string title, int year)
        {
            MovieID = id;
            Title = title;
            Year = year;
        }
    }

}

我的创建视图:

@model MovieSite.Models.Movie
@{
    ViewBag.Title = "Create Movie";
}
<h2>Add Movie</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <label style="color:red" id="errormsg"></label>
        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", id = "movieTitle" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Year, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Year, new { htmlAttributes = new { @class = "form-control", id = "movieYear" } })
                @Html.ValidationMessageFor(model => model.Year, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Genres, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10" style="height:250px; width:400px; overflow-y:scroll">
                <br />
                @{
                    List<MovieSite.Models.GenreToMovie> moviegenres = ViewBag.Genres;
                    foreach (var genre in moviegenres.OrderBy(m => m.Name))
                    {
                        <ul style="padding:0px">
                            <input type="checkbox" name="SelectedGenres" value="@genre.id" @Html.Raw(genre.Checked ? "checked=\"checked\"" : "") />
                            @genre.Name
                        </ul>
                    }
                }
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default"/>
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Manage")
</div>

你可以在我的观点中看到一半我有一个视图包:

@{
                    List<MovieSite.Models.GenreToMovie> moviegenres = ViewBag.Genres;
                    foreach (var genre in moviegenres.OrderBy(m => m.Name))
                    {
                        <ul style="padding:0px">
                            <input type="checkbox" name="SelectedGenres" value="@genre.id" @Html.Raw(genre.Checked ? "checked=\"checked\"" : "") />
                            @genre.Name
                        </ul>
                    }
                }

显示我的类型,我不希望这样。我试图制作一些用于组合数据的视图模型,但我无处可去。我已经在桌子上撞了好几天了,在网上找不到明确的例子。请欧比万,你是我唯一的希望。

我只需要一个干净的例子,我可以解决这个问题。我现在无法解锁它。感谢您的时间和帮助。

编辑:

根据评论中的建议,我创建了一个viewmodel来通过: 公共类MovieAddVM

{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Year { get; set; }
    public string Title { get; set; }
    public bool Checked { get; set; }

    public virtual ICollection<Genre> Genres { get; set; }

    public MovieAddVM()
    {
        GenreList = new List<GenreToMovie>();
    }
    public List<GenreToMovie> GenreList { get; set; }

}

它注册了我的观点,但没有填充一个类型列表。如何查询我的流派并将该列表归因于viewmodel?对不起,我对这一切感到非常困惑。

0 个答案:

没有答案