0

我正在尝试在 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();
    } 

我遇到问题的 Create 视图部分是:

    <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 设置为空。

4

1 回答 1

0

You can probably tell I'm quite new to this but I have actually solved my problem. I did this by firstly changing the dropdownlist helpers to dropdownlistfor to enable it to save back:

    <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>

This returned me an album with initialised Artist and Genre properties. The only thing was that they only contained Id properties so I had to use these to get the rest of the properties (using nhibernate) before saving:

    [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);
        }
于 2012-04-13T14:41:45.920 回答