.Net 6 MVC - ViewModel - 在视图中显示第二个属性

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

前提

我正在尝试使用一个非常简化的 .net 6 MVC 项目来了解如何编写 ViewModels。

项目缩略图

模型 A 乙 C(入班)

视图模型 AB


**GitHub**

环境: Net Core 6 MVC, Visual Studio Community 2022 (64 bit), WIndows 11

:只要有可能,请以实际代码的形式提供帮助 - 最好是对我提供的代码进行更改(提供此代码作为问题的前提需要花费大量时间和精力) - 或者例如您提供的代码,在任何一种情况下,都以代码需要的格式/语法来制作项目。抽象往往会混淆答案。


问题

为什么显示第一个属性的数据而不显示第二个属性的数据?如何更改控制器操作方法代码(下方)以同时显示来自第二个属性的数据?

Controller Action Method成功显示如图(上图)

        //GET: AB
        public IActionResult AB2()
        {
            //define a variable to store the return data.
            var ab2 = new List<AB>();
            //query A and B table, 
            var Alist = _context.A.ToList();
            var Blist = _context.B.ToList();
            //use foreach statement to filter data. And Add A and B to the ab list.
            foreach (var item in Alist)
            {
                var newab = new AB()
                {
                    A = item
                };
                //add the new item into the list.
                ab2.Add(newab);
            }
            return View(ab2); // return the list to the view page.
        }

**完整代码**

模型

A

    namespace ViewModel.Models
    {
    public class A
        {
        public int Id { get; set; }

        public string? One { get; set; }

        public string? Two { get; set; }

        public string? Three { get; set; }

        public IEnumerable<C>? C { get; set; }

        }
   }

B

namespace ViewModel.Models
{
    public class B
    {
        public int Id { get; set; }

        public string? One { get; set; }

        public string? Two { get; set; }

        public string? Three { get; set; }

        public IEnumerable<C>? C { get; set; }
    }
}

C

using System.Security.Cryptography.Xml;

namespace ViewModel.Models
{
    public class C
    {
        public int Id { get; set; }

        public int AId { get; set; }

        public int BId { get; set; }

        // _______________________________________

        public A? A { get; set; }
        public B? B { get; set; }
    }
}

上下文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ViewModel.Models;

namespace ViewModel.Data
{
    public class ViewModelContext : DbContext
    {
        public ViewModelContext (DbContextOptions<ViewModelContext> options)
            : base(options)
        {
        }

        public DbSet<ViewModel.Models.A> A { get; set; } = default!;

        public DbSet<ViewModel.Models.B>? B { get; set; }

        public DbSet<ViewModel.Models.C>? C { get; set; }
    }
}

视图模型

using System.Security.Cryptography.Xml;

namespace ViewModel.Models.View
{
    public class AB
    {
        public A? A { get; set; }

        public B? B { get; set; }
    }
}

查看

@model IEnumerable<ViewModel.Models.View.AB>

@{
    ViewData["AB2"] = "AB2";
}

<h1>AB2</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                Property Set A, Property @Html.DisplayNameFor(model => model.A.One)
            </th>
            <th>
                Property Set B, Property @Html.DisplayNameFor(model => model.B.One)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.A.One)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.B.One)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.A.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.A.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.A.Id">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>

我创建了一个简化的项目作为这个问题的前提。我有一个通过视图模型派生的工作视图,该视图模型包含两个类中每个类的属性。我现在的问题是关于第二个视图,它在控制器中的编码与显示一个类的属性的第一个视图不同。我正在尝试从第二个类中获取第二个属性来显示。


我已经尝试对控制器操作方法代码进行一些更改。都失败了:

        //GET: AB
    public IActionResult AB2()
    {
        //define a variable to store the return data.
        var ab2 = new List<AB>();
        //query A and B table, 
        var Alist = _context.A.ToList();
        var Blist = _context.B.ToList();
        //use foreach statement to filter data. And Add A and B to the ab list.
        foreach (var item in Alist)
        {
            var newab = new AB()
            {
                A = item
              //B = item                  **2nd change: did not work**
            };
            //add the new item into the list.
            ab2.Add(newab);
        }

        //foreach (var item in Blist)    **1st change: did not work**
        //{
        //    var newab = new AB()
        //    {
        //        B = item
        //    };
        //    //add the new item into the list.
        //    ab2.Add(newab);
        //}

        return View(ab2); // return the list to the view page.
    }
asp.net-core-mvc asp.net-mvc-viewmodel
© www.soinside.com 2019 - 2024. All rights reserved.