'IEnumerable <alert>'不包含'Alert_Identifier'的定义...并且'IEnumerable <alert>'不包含'AlertIndex'的定义

时间:2018-08-08 18:12:12

标签: asp.net asp.net-mvc

我已经看过许多类似的问题,但是我仍然陷入困境。我已将Alert.cs类更改为从IEnumerable继承,但是当Visual Studio编译项目以使用Alert_Identifier和AlertIndex填充“选择标记帮助器”时,这并没有解决问题。这是课程。

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中。请告诉我为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

您的视图的类型严格为IEnumerable<YourClass>,在您的视图中,您正在传递视图模型的AlertIndex属性,以使用选择标记帮助器构建SELECT元素。但是IEnumerable集合没有AlertIndex属性!因此,您会收到错误消息。

无需从Alert继承您的IEnumerable类。只需删除它即可。

您应该创建一个视图模型,该模型具有视图所需的特定属性。要呈现SELECT元素,请添加2个属性,一个属性用于所选选项,另一个属性用于构建SELECT Options所需的项目集合。

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表达式来获取Alert集合。

@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对象的列表。因此,如果您不渲染它,那就根本不要查询它!