在MVC中使用Fluent NHibernate在Create View中映射对象

时间:2012-04-11 10:34:33

标签: asp.net-mvc asp.net-mvc-3 nhibernate asp.net-mvc-2 fluent-nhibernate

我正在尝试在MvcMusicStore示例中使用Fluent NHibernate而不是Entity Framework,并且在使用Create View创建新专辑时填充ArtistId和GenreId时遇到问题。我认为这与我引用其他对象的事实有关

我的相册地图是:

    public class AlbumMap:ClassMap<Album>
    {
        public AlbumMap()
        {
            Id(x => x.AlbumId);
            Map(x => x.AlbumArtUrl);
            References(x => x.Artist).Cascade.All().Column("ArtistId");
            References(x => x.Genre).Cascade.All().Column("GenreId");
            Map(x => x.Price);
            Map(x => x.Title);
        }
    }

控制器中的Create方法如下所示:

    public ActionResult Create()
    {
        MusicRepository repository = new MusicRepository();
        ViewBag.ArtistId = new SelectList(repository.GetArtists(), "ArtistId", "Name");
        ViewBag.GenreId = new SelectList(repository.GetGenres(), "GenreId", "Name");
        return View();
    } 

和我遇到问题的创建视图部分是:

    <div class="editor-label">
        @Html.LabelFor(model => model.Genre, "Genre")
    </div>
    <div class="editor-field">
        @Html.DropDownList("GenreId", String.Empty)
        @Html.ValidationMessageFor(model => model.Genre)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Artist, "Artist")
    </div>
    <div class="editor-field">
        @Html.DropDownList("ArtistId", String.Empty)
        @Html.ValidationMessageFor(model => model.Artist)
    </div>

在数据库中,创建新相册后,其他字段(如Title和AlbumUrl)将被填充,但ArtistId和GenreId将设置为null。

1 个答案:

答案 0 :(得分:0)

你可能会说我对此很新,但实际上我已经解决了我的问题。我这样做是首先将dropdownlist帮助器更改为dropdownlistfor以使其能够保存:

    <div class="editor-label">
        @Html.LabelFor(model => model.Genre, "Genre")
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Genre.GenreId, (SelectList)ViewBag.GenreId,"Please select")
        @Html.ValidationMessageFor(model => model.Genre.GenreId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Artist, "Artist")
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Artist.ArtistId, (SelectList)ViewBag.ArtistId,"Please select")
        @Html.ValidationMessageFor(model => model.Artist.ArtistId)
    </div>

这给了我一张初始化艺术家和流派属性的专辑。唯一的事情是它们只包含Id属性,因此在保存之前我必须使用它们来获取其余的属性(使用nhibernate):

    [HttpPost]
    public ActionResult Create(Album album)
    {
        try
        {
            MusicRepository repository = new MusicRepository();
            if (ModelState.IsValid)
            {
                album.Artist = repository.GetArtistById(album.Artist.ArtistId);
                album.Genre = repository.GetGenreById(album.Genre.GenreId);
                repository.AddAlbum(album);
                return RedirectToAction("Index");  
            }
            ViewBag.ArtistId = new SelectList(repository.GetArtists(), "ArtistId", "Name");
            ViewBag.GenreId = new SelectList(repository.GetGenres(), "GenreId", "Name");
            return View(album);
        }