[C#在一个视图中使用2个模型的上下文

问题描述 投票:0回答:1

我需要在一个视图中使用2个模型的上下文的帮助。我需要使用ZanrID中的ImeZanrModel.cs

这里是ZanrModel.cs代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    [Table("Zanr")]
    public class ZanrModel

    {
        [Key]
        public int ZanrID { get; set; }

        public string Ime { get; set; }

    }
}

我想在此视图中使用ZanrdID使用ImeKnjigaModel.cs

@model IEnumerable<WebApplication1.Models.KnjigaModel>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>Index</h2>

<a href="/Knjiga/Create" class="btn btn-warning">Dodaj novu knjigu</a>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.InventarniBroj)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Pisac)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Naslov)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.GodinaIzdavanja)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MestoIzdavanja)
        </th>
        <th>
        </th>
        <th>
        </th>
        <th>
        </th>
    </tr>

    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.InventarniBroj)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Pisac)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Naslov)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.GodinaIzdavanja)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MestoIzdavanja)
        </td>
        <td>
            <a href="/Knjiga/Izmeni/@item.KnjigaId" class="btn btn-warning">Edit</a>
        </td>
        <td>
            <a href="/Knjiga/Details/@item.KnjigaId" class="btn btn-primary">Details</a>
        </td>
        <td>
            <a href="/Knjiga/Delete/@item.KnjigaId" class="btn btn-danger">Delete</a>
        </td>
    </tr>
    }

</table>

是,我想在dropdown menu中创建Knjiga/Create.cshtml,用户可以在其中选择什么Zanr他们想使用它。我有KnjigaZanr的数据库,这是它的图片:

dbo.Knjiga table

enter image description here

dbo.Zanr table

enter image description here

提前感谢!

c# html asp.net-mvc database
1个回答
1
投票

我相信最好的方法是创建一个ViewModel对象以包含两个组合的Model对象。

将模型直接放在视图上不是一个好习惯。

//New class outside the view.
Public class KnjigaZanrVM 
{
IEnumerable<KnjigaModel> Knjiga {get} 
IEnumerable<ZanrModel> Zanr {get}
}

//on the View
@model WebApplication1.ViewModel.KnjigaZanrVM

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
...
@foreach (var item in Model.Knjiga)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.InventarniBroj)
        </td>
        <td>
...
foreach (var item in Model.Zanr)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ZanrID)
        </td>
        <td>
...

查看此链接:

https://www.c-sharpcorner.com/article/managing-data-with-viewmodel-in-asp-net-mvc/

https://www.aspsnippets.com/Articles/Pass-Send-Object-from-View-to-Controller-in-ASPNet-MVC.aspx

© www.soinside.com 2019 - 2024. All rights reserved.