我已经看过许多类似的问题,但我仍然被卡住了。我已经将我的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中。请告诉我为什么这个不起作用。
发布于 2018-08-09 02:28:35
您的视图被强类型化为IEnumerable<YourClass>,并且在您的视图中,您传递视图模型的AlertIndex属性来构建带有选择标记助手的选择元素。但是IEnumerable集合没有AlertIndex属性!因此,你得到了这个错误。
不需要从IEnumerable继承Alert类。去掉它就好。
您应该创建一个视图模型,该模型具有视图所需的特定属性。为了呈现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对象列表。所以,如果你不渲染它,那就根本不要查询它!
https://stackoverflow.com/questions/51753145
复制相似问题