asp.net - 'IEnumerable' 不包含 'Alert_Identifier' ... 和 'IEnumerable 的定义' 不包含 'AlertIndex' 的定义
问题描述
我看过很多类似的问题,但我仍然卡住了。我已将我的 Alert.cs 类更改为从 IEnumerable 继承,但是当 Visual Studio 编译项目以使用 Alert_Identifier 和 AlertIndex 来填充 Select Tag Helper 时,这并没有解决问题。这是课程。
Alert.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace edxl_cap_v1_2.Models
{
public class Alert : IEnumerable
{
[Key]
public int AlertIndex { get; set; }
[MaxLength(150)]
public string Alert_Identifier { get; set; }
public string Sender { get; set; }
public DateTime Sent { get; set; }
public Status Status { get; set; }
public MsgType MsgType { get; set; }
public string Source { get; set; }
public Scope Scope { get; set; }
public string Restriction { get; set; }
public string Addresses { get; set; }
public string Code { get; set; }
public string Note { get; set; }
public string References { get; set; }
public string Incidents { get; set; }
public int DataCategory_Id { get; set; }
public ICollection<Element> Elements { get; set; }
public System.Collections.IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
public enum Status
{
Actual,
Exercise,
System,
Test,
Draft
}
public enum MsgType
{
Alert,
Update,
Cancel,
Ack,
Error
}
public enum Scope
{
Public,
Restricted,
Private
}
}
这是 _CapCategoryLayout.cshtml 中发生错误的地方
@model IEnumerable<edxl_cap_v1_2.Models.Alert>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="~/css/capv1_2_refimp.css" />
<title>@ViewBag.Title</title>
</head>
<body>
<!-- Header -->
<header>
<div class="content-wrapper">
@Html.Partial("_EdxlHeader")
</div>
@{
<form asp-controller="EdxlCapMsg" asp-action="Index" method="post" class="form-horizontal" role="form">
<div class="form-group">
<div class="alert-danger" asp-validation-summary="ModelOnly"></div>
<div class="content-wrapper">
<span class="smallText">
<label asp-for="Alert_Identifier" class="control-label"></label>
<select asp-for="AlertIndex" class="form-control"
asp-items="@(new SelectList(@ViewBag.ListofIdentifier,"AlertIndex", "Alert_Identifier"))"></select>
</span>
</div>
</div>
<div class="form-group">
<div class="content-wrapper">
<input id="Submit1" type="submit" value="submit" />
</div>
</div>
<div class="form-group">
<div class="content-wrapper">
@if (ViewBag.SelectedValue != null)
{
<text>Selected Alert_Identifier: </text> @ViewBag.SelectedValue;
}
</div>
</div>
</form>
}
</header>
<!-- End of Header -->
<!-- edxlLeftColumn -->
<div id="edxlLeftColumn">
@Html.Partial("_CapLeftColumnPartial")
</div>
<!-- End of edxlLeftColumn -->
<!-- indexRightColumn-Content Body -->
<div id="indexRightColumn" style="position:relative">
@RenderBody()
@RenderSection("scripts", required: false)
</div>
<!-- End of edxlRightColumn-Content Body -->
<!-- Footer -->
<footer>
<div class="content-wrapper">
@Html.Partial("_EdxlFooter")
</div>
</footer>
这是控制器 EdxlCapMsgController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using edxl_cap_v1_2.Data;
using edxl_cap_v1_2.Models;
using edxl_cap_v1_2.Models.ContentViewModels;
namespace edxl_cap_v1_2.Controllers
{
public class EdxlCapMsgController : Controller
{
private readonly ApplicationDbContext _context;
public EdxlCapMsgController(ApplicationDbContext context)
{
_context = context;
}
// GET: EdxlCapMessages
public async Task<IActionResult> Index()
{
List<EdxlCapMsg> identifierlist = new List<EdxlCapMsg>();
//------Getting Data fom Database using EntityFrameworkCore------
identifierlist = (from product in _context.EdxlCapMsg
select product).ToList();
//------Inserting Select Item in List------
identifierlist.Insert(0, new EdxlCapMsg { Id = 0, Alert_Identifier = "Select" });
//------Assigning countrylist to ViewBag.ListofCountry------
ViewBag.ListofIdentifier = identifierlist;
return View(await _context.EdxlCapMsg.ToListAsync());
}
[HttpPost]
public IActionResult Index(EdxlCapMsg EdxlCapMsg)
{
//------Validation------
if (EdxlCapMsg.Id == 0)
{
ModelState.AddModelError("", "Select EdxlCapMsg");
}
//------Getting selected value------
int SelectedValue = EdxlCapMsg.Id;
ViewBag.SelectedValue = EdxlCapMsg.Id;
//------Setting Data back to ViewBag after Posting form------
List<EdxlCapMsg> identifierlist = new List<Models.EdxlCapMsg>();
identifierlist = (from product in _context.EdxlCapMsg
select product).ToList();
identifierlist.Insert(0, new EdxlCapMsg { Id = 0, Alert_Identifier = "Select" });
ViewBag.ListofIdentifier = identifierlist;
return View();
}
}
我真的认为在 Alert.cs 中显式使用 ': IEnumerable' 将确保 AlertIndex 和 Alert_Identifier 将包含在 IEnumerable 中。请告诉我为什么这不起作用。
解决方案
您的视图是强类型的,IEnumerable<YourClass>
并且在您的视图中,您正在传递AlertIndex
视图模型的属性以使用选择标记帮助器构建 SELECT 元素。但是IEnumerable
collection没有AlertIndex
属性!因此你得到了错误。
无需Alert
从IEnumerable
. 只需删除它。
您应该创建一个具有视图所需的特定属性的视图模型。为了呈现 SELECT 元素,添加 2 个属性,一个用于选定选项,一个用于构建 SELECT 选项所需的项目集合。
public class MyListVm
{
public string SelectedIdentifier { set;get;}
public List<SelectListItem> Identifiers { set;get;}
public List<AlertVm> Alerts { set;get;}
}
public class AlertVm
{
public string Source { set;get;}
public string Sender { set;get;}
// Add other properties needed by the view.
}
在您的 GET 操作中,您将创建此类的一个对象MyListVm
,填充Identifiers
集合属性和Alerts
属性并将其发送到视图。
public ActionResult Index()
{
var vm = new MyListVm();
//Load the Identifiers property which will be used to build the SELECT element
vm.Identifiers = _context.EdxlCapMsg
.Select(x=>new SelectListItem { Value =x.Id.ToString(),
Text=x.Name}).ToList();
//Load the Alerts
vm.Alerts = _context.EdxlCapMsg.Select(a=>new AlertVm {
Source = a.Source,
Sender = a.Sender }).ToList();
return View(vm);
}
现在确保你的视图是强类型到这个视图模型的。您可以使用Model.Alerts
表达式来获取警报集合。
@model MyListVm
<h2>@Model.Alerts.Count alerts</h2>
<form>
<select asp-for="SelectedAlertIndex" asp-items="@Model.AlertOptions">
<option>Select one</option>
</select>
<input id="Submit1" type="submit" value="submit" />
</form>
进行必要的更改(根据属性名称)以使其适用于您的用例。此外,您在问题中共享的代码似乎没有使用Alert
对象列表。因此,如果您不渲染它,则根本不要查询它!
推荐阅读
- macos - 如何使用 AppleScript 关闭 Safari 偏好设置窗口?
- excel - 如何使用 IF 以及 AND 和多个 OR 条件创建 Excel 公式
- ajax - 每秒使用 Ajax 请求有多安全
- python - python的subprocess.Popen()实时刷新颜色输出
- makefile - Make:递归后删除目标
- python - 将按键事件绑定到标签小部件 tkinter
- android - Android Studio“文件以错误的编码加载:'UTF-8'”和“Java文件未声明任何类”无处不在
- azure - 如何在通过 Azure 数据工厂将数据从 Azure Cosmos DB 复制到 ADLS Gen 2 时设置输出路径
- python - 希望创建显示每个月值的面板数据。需要帮助填写没有值的月份
- c - 获取一个二维数组并将其合并为一个