【发布时间】:2021-12-22 13:48:02
【问题描述】:
我有 2 个模型; TypeOne 和 Project_Screen。我需要一个视图来制作两个表,其中包含来自这两个表的信息。我尝试使用本指南来制作一个视图模型,它有帮助但没有做与我完全相同的事情:https://dotnettutorials.net/lesson/view-model-asp-net-core-mvc/
这是我制作的视图模型:
public class MyProjectsViewModel
{
public Project_Screen Project_Screen { get; set; }
public TypeOne TypeOne { get; set; }
}
这是控制器:
public class ProfileController : Controller
{
private readonly Natural_ResourcesContext _context;
public ProfileController(Natural_ResourcesContext context)
{
_context = context;
}
// GET: Profile
public ActionResult Index()
{
Project_Screen project_Screen = (Project_Screen)(from s in _context.Project_Screen
where s.DSN_PM == User.Identity.Name
select s);
TypeOne typeOne = (TypeOne)(from x in _context.TypeOne
where x.Name == User.Identity.Name
select x);
MyProjectsViewModel myProjectsViewModel = new MyProjectsViewModel()
{
Project_Screen = project_Screen,
TypeOne = typeOne
};
return View(myProjectsViewModel);
}
}
如您所见,project_Screen 和typeOne 都是Manager = Name 所在的记录。
然后我想在视图中显示如下内容:
@model EnvApp.ViewModels.MyProjectsViewModel
@{
ViewData["Title"] = "Projects";
}
<h1>My Projects</h1>
<hr />
<h4>Screening Forms</h4>
<table class="table">
<thead>
<tr>
<th>
State Project Number
</th>
<th>
Federal Project Number
</th>
<th>
Project Name
</th>
<th>
County
</th>
<th>
Coordinates
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.State_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Federal_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.County)
</td>
<td>
@Html.DisplayFor(modelItem => item.Coordinates)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.ID">Details</a>
</td>
</tr>
}
</tbody>
</table>
<br />
<hr />
<h4>Type One Projects</h4>
<table class="table">
<thead>
<tr>
<th>
State Project Number
</th>
<th>
Federal Project Number
</th>
<th>
Project Name
</th>
<th>
County
</th>
<th>
Coordinates
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.State_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Federal_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.County)
</td>
<td>
@Html.DisplayFor(modelItem => item.Coordinates)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.ID">Details</a>
</td>
</tr>
}
</tbody>
</table>
我在 ````Model``` 上得到编译器错误 CS1579:
foreach statement cannot operate on variables of type 'MyProjectsViewModel' because 'MyProjectsViewModel' does not contain a public instance or extension definition for 'GetEnumorator'
我认为这是有道理的,因为我没有将 Project_Screen 或 TypeOne 表示为包含多个记录的对象,但我不太确定如何执行此操作。
我是在正确的轨道上还是我误解了这一点?我错过了什么?
编辑 1:
我完全按照 Md Farid Uddin Kiron 所说的去做,所以他回答中的代码反映了我的视图模型和视图。但是我的控制器仍然存在问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using EnvApp.ViewModels;
using EnvApp.Models.DB;
namespace EnvApp.Controllers
{
public class ProfileController : Controller
{
private readonly Natural_ResourcesContext _context;
public ProfileController(Natural_ResourcesContext context)
{
_context = context;
}
// GET: Profile
public ActionResult Index()
{
Project_Screen project_Screen = (Project_Screen)(from s in _context.Project_Screen
where s.DSN_PM == User.Identity.Name
select s);
TypeOne typeOne = (TypeOne)(from x in _context.TypeOne
where x.Name == User.Identity.Name
select x);
MyProjectsViewModel myProjectsViewModel = new MyProjectsViewModel()
{
Project_Screen = project_Screen,
TypeOne = typeOne
};
return View(myProjectsViewModel);
}
}
}
问题在于 project_Screen 和 typeOne 不是列表或 IEnumerable 类型,这是有道理的,但我不是让它们可枚举的语法。
如果您需要查看,这是我的视图模型:
public class MyProjectsViewModel
{
public List<Project_Screen> Project_Screen { get; set; }
public List<TypeOne> TypeOne { get; set; }
}
这是视图:
<h1>My Projects</h1>
<hr />
<h4>Screening Forms</h4>
<table class="table">
<thead>
<tr>
<th>
State Project Number
</th>
<th>
Federal Project Number
</th>
<th>
Project Name
</th>
<th>
County
</th>
<th>
Coordinates
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Project_Screen)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.State_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Federal_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.County)
</td>
<td>
@Html.DisplayFor(modelItem => item.Coordinates)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.ID">Details</a>
</td>
</tr>
}
</tbody>
</table>
<br />
<hr />
<h4>Type One Projects</h4>
<table class="table">
<thead>
<tr>
<th>
State Project Number
</th>
<th>
Federal Project Number
</th>
<th>
Project Name
</th>
<th>
County
</th>
<th>
Coordinates
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.TypeOne)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.State_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Federal_Project_Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.County)
</td>
<td>
@Html.DisplayFor(modelItem => item.Coordinates)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.ID">Details</a>
</td>
</tr>
}
</tbody>
</table>
同样,问题在于project_Screen 和typeOne 不是控制器中的列表或IEnumerable 类型。它们实际上是列表,除了控制器之外的任何地方,但它们必须是。错误就在上面
MyProjectsViewModel myProjectsViewModel = new MyProjectsViewModel()
{
Project_Screen = project_Screen,
TypeOne = typeOne
};
在project_Screen 和typeOne 上并阅读
cannot implicitly convert type Envapp.Models.DB.Project_Screen to System.Collection.Generic.List<Envapp.Models.DB.Project_Screen>
这似乎正在发生,因为那里的代码试图指向 Project_Screen 的 DB 模型表,而不是 Project_Screen 模型中项目的 ViewModel 表示。我该如何解决这个问题?
编辑 2:
我不确定它是什么,但我的代码中的某些内容似乎真的让人们感到困惑。我发布了我所有的模型以供澄清:
Project_Screen.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
#nullable disable
namespace EnvApp.Models.DB
{
public partial class Project_Screen
{
[Key]
public long ID { get; set; }
public string State_Project_Number { get; set; }
public string? Federal_Project_Number { get; set; }
public string Project_Name { get; set; }
public string County { get; set; }
public DateTime? Memo_Date { get; set; }
public string From { get; set; }
public string? Authorization { get; set; }
public string DSN_PM { get; set; }
public string? History { get; set; }
public string History_PM { get; set; }
public bool Review_Exempt_H { get; set; }
public bool SHPO_Approval_H { get; set; }
public string? Archaeology { get; set; }
public string Archaeology_PM { get; set; }
public bool Review_Exempt_A { get; set; }
public bool SHPO_Approval_A { get; set; }
public bool ESA_Key { get; set; }
public bool Crayfish { get; set; }
public bool Crayfish_Habitat_Assessment { get; set; }
public bool NLEB_4D { get; set; }
public bool USFWS { get; set; }
public string USFWS_Type { get; set; }
public bool Mussel_Habitat { get; set; }
public bool Mussel_Stream { get; set; }
public string Within_Airport { get; set; }
public string? ToPo_Quad_Name { get; set; }
public bool Bat_Habitat { get; set; }
public string? Bars { get; set; }
public string Coordinates { get; set; }
public string? Natural_Resources_Notes { get; set; }
public string Adduser { get; set; }
public DateTime? Date_Added { get; set; }
public string Crayfish_Notes { get; set; }
public string Mussel_Notes { get; set; }
}
}
TypeOne.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
#nullable disable
namespace EnvApp.Models.DB
{
[Table("Type_One")]
public partial class TypeOne
{
[Key]
public long ID { get; set; }
[MaxLength(50)]
public string State_Project_Number { get; set; }
[MaxLength(50)]
public string Federal_Project_Number { get; set; }
[MaxLength(50)]
public string Name { get; set; }
[MaxLength(50)]
public string Route_Number { get; set; }
public string County { get; set; }
[MaxLength(50)]
public string Work_Type { get; set; }
[MaxLength(100)]
public string Coordinates { get; set; }
public string Project_Description { get; set; }
public bool? Federal_Aid { get; set; }
public bool? Minimal_Project_Verification { get; set; }
[MaxLength(3)]
public string CE_Category { get; set; }
[MaxLength(10)]
public string Amms { get; set; }
public bool Activities_Agreement { get; set; }
public string Arch_RE { get; set; }
public string Hist_RE { get; set; }
public DateTime? Arch_RE_Date { get; set; }
public DateTime? Hist_RE_Date { get; set; }
public bool? Through_Lanes { get; set; }
public bool? Close_Road { get; set; }
public bool? ROW_Acquisition { get; set; }
public bool? Access_Control { get; set; }
public bool? Fifty_Year_Structure { get; set; }
public bool? Agency_Coordination { get; set; }
public bool? IPAC_Screening_Zone { get; set; }
public bool? Section_404_Permit { get; set; }
public bool? Ground_Disturbance { get; set; }
public bool? Waterway { get; set; }
public bool? Special_Use_Permit { get; set; }
public bool? Floodplain { get; set; }
public string Prepared_By { get; set; }
public string Approved_By { get; set; }
public string Adduser { get; set; }
public DateTime? Date_Added { get; set; }
}
}
Natural_ResourcesContext
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
#nullable disable
namespace EnvApp.Models.DB
{
public partial class Natural_ResourcesContext : DbContext
{
public Natural_ResourcesContext()
{
}
public Natural_ResourcesContext(DbContextOptions<Natural_ResourcesContext> options)
: base(options)
{
}
public virtual DbSet<NR_User> NR_Users { get; set; }
public virtual DbSet<Project_Screen> Project_Screen { get; set; }
public virtual DbSet<TypeOne> TypeOne { get; set; }
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
【问题讨论】:
-
你为什么要在这里拆箱?
Project_Screen project_Screen = (Project_Screen)(from s in...。您不能将可枚举类型拆箱为这样的单一类型。尝试删除(Project_Screen)。 -
我不知道。如果我删除
(Project_Screen),查询会出现错误:Cannot implicitly convert type System.Link.IQueryable<Envapp.Models.DB.Project_Screen> to Envapp.Models.DB.Project_Screen. An explicit conversion exists.,这是唯一的建议,所以当我单击添加显式转换时,它会这样做,然后查询就会起作用。如何以正确的方式纠正此错误? -
将模型属性更改为:
public IEnumerable<Project_Screen> Project_Screen { get; set; } -
这会破坏我使用 Project_Screen 的其他地方的项目......我将发布我所有的模型,因为我认为有些东西让人们感到困惑。
-
第二个。它们是同一个的编辑。
标签: c# asp.net-core-mvc viewmodel asp.net-core-5.0