簡體   English   中英

在ViewModel中將值從一個模型傳遞到另一個模型

[英]Passing value from one model to another in a ViewModel

我正在構建我的第一個MVC網站,我正在努力在ViewModel中將值從一個模型傳遞到另一個模型。 這兩個課程是Music and MusicGenre。 在Music中,有一個名為'GenreId'的字段,它是一個int,與MusicGenres中的'Id'字段相關。 我想返回與傳遞的Id相關的類型名稱。

這是我到目前為止所得到的:

        MusicViewModel vmMusic = new MusicViewModel
        {
            Music = _context.Music.SingleOrDefault(c => c.Id == id),
            MusicGenres = _context.MusicGenres.Where(gi => gi.Id == xxx).ToList()
        };
        return View(vmMusic); 

這一切都很好地減去顯示正確的流派。 (我用Id替換了xxx,但只使用了Music中的id字段,而不是GenreId字段)

所以xxx是我想傳遞'GenreId'但我不知道怎么做的地方。 有幫助嗎? 謝謝

為了擴展我對這個問題的初步評論,我只是做了類似的事情:

    var music = _context.Music.SingleOrDefault(c => c.Id == id);
    var musicGenres = _context.MusicGenres.Where(gi => gi.Id == music.GenreId).ToList(); // whatever the genre id property is called.
    MusicViewModel vmMusic = new MusicViewModel
    {
        Music = music,
        MusicGenres = musicGenre
    };
    return View(vmMusic); 

您應該將視圖模型與持久性模型分開。 視圖模型旨在顯示您要向用戶顯示的內容。 持久性模型可能反映您的數據存儲結構。

同樣從您的示例看起來您可能已經使用了像EntityFramework這樣的ORM。 EntityFramework提供導航屬性和延遲加載等功能。

無論哪種方式,我強烈不同意接受的答案的方法,當你只需要獲得流派名稱時,它將音樂及其類型持久性模型發送到視圖中。

相反,我會這樣做:

持久性模型

您可能在持久性存儲中的類型和相冊之間存在一對一的關系:

public class Album
{
    public int AlbumId { get; set; }
    public string Title { get; set; }
    public double Price { get; set; }

    public int GenreId { get; set; }
    public Genre Genre { get; set; }
}

public class Genre
{
    public int GenreId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public List<Album> Albums { get; set; }
}

但顯然您不希望/需要將持久存儲中的所有內容透露給用戶。 因此,您創建視圖模型並包含要顯示的內容。

查看模型

假設您有一個顯示所選專輯信息的視圖。 您可以創建這樣的視圖模型:

public class AlbumViewModel
{
    public int AlbumId { get; set; }
    public string AlbumName { get; set; }
    public string Genre { get; set; }
}

然后在相冊控制器中,您可以使用任何ORM,延遲加載等構建視圖模型:

調節器

如果您碰巧使用EntityFramework並且啟用了延遲加載,則可以通過其導航屬性獲取流派名稱:

public ActionResult Details(int id)
{
    var album = _dbContext.Albums.SingleOrDefault(x => x.AlbumId == id);
    if (album == null)
    {
        return HttpNotFound();
    }

    var vm = new AlbumViewModel
    {
        AlbumId = album.AlbumId,
        AlbumName = album.Title,

        // You can get the genre name via Navigation property, and/or lazy
        // loading
        Genre = album.Genre.Name
    };

    return View(vm);
}

現在,在更高級的體系結構中,讀取和寫入是分開的,稱為CQRS。 對於所有讀取(即向用戶顯示信息),您可以直接從執行純SQL語句構建包含數據的視圖模型。

CQS與Dapper

using Dapper;
using System.Data;

...

public class AlbumController : Controller
{
    private readonly IDbConnection _dbConnection;

    public AlbumController(IDbConnection dbConnection)
    {
        _dbConnection = dbConnection;
    }

    public ActionResult Details(int id)
    {
        const string sql = @"
SELECT a.AlbumId, a.Title AS [AlbumName], g.Name AS [Genre]
FROM [Album] a
    JOIN [Genre] g ON a.GenreId = g.GenreId
WHERE a.AlbumId = @albumId;
";

        var vm = _dbConnection.QuerySingleOrDefault<AlbumViewModel>(sql, new {
            albumId = id
        });
        if (vm == null)
        {
           return HttpNotFound();
        }

        return View(vm);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM