在剃须刀页面中放置一个下拉列表

时间:2020-09-10 23:26:00

标签: razor razor-pages

我想在页面上添加一个下拉菜单。我有以下模型类:

using System;
using System.Collections.Generic;

namespace ProjectList.Models
{
    public partial class AllItProjectsList
    {
        public int ProjectId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public IEnumerable<ProjectType> Type { get; set; }
     
       
    }
}

using System;
using System.Collections.Generic;

namespace ProjectList.Models
{
    public partial class ProjectType
    {
        public int ProjectTypeId { get; set; }
        public string ProjectType1 { get; set; }
    }
}

在我的CSHTML页面上,我有如下代码:

<div class="form-group">
                <label asp-for="AllItProjectsList.Type" class="control-label"></label>
                <input asp-for="AllItProjectsList.Type" class="form-control" />
                <span asp-validation-for="AllItProjectsList.Type" class="text-danger"></span>
            </div>

我将上面的代码更改为此,以便放置下拉列表:

<div class="form-group">
                <label asp-for="AllItProjectsList.Type" class="control-label"></label>
                @Html.DropDownListFor(x => Model.AllItProjectsList.Type, new SelectList(Model.AllItProjectsList.Type, "ProjectTypeId", "ProjectType1"), htmlAttributes: new { @class = "form-control", id = "Type1" })
            </div>

我在ONModelCreating中具有以下代码:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
         modelBuilder.Entity<AllItProjectsList>(entity =>
        {
            entity.HasKey(e => e.ProjectId);

            entity.ToTable("All_IT_Projects_List");

            entity.Property(e => e.ProjectId).HasColumnName("Project_ID");

             entity.Property(e => e.Type).HasMaxLength(255);
        }
     });
 
 
 
 
 modelBuilder.Entity<ProjectType>(entity =>
        {
            entity.ToTable("Project_Type");

            entity.Property(e => e.ProjectType1)
                .HasColumnName("ProjectType")
                .HasMaxLength(50)
                .IsUnicode(false);
        });  
        OnModelCreatingPartial(modelBuilder);

我收到此错误:

InvalidOperationException:属性“ AllItProjectsList.Type”的类型为“ IEnumerable”,当前数据库提供程序不支持该类型。可以使用“ [NotMapped]”属性或“ OnModelCreating”中的“ EntityTypeBuilder.Ignore”来更改属性CLR类型或忽略该属性。

0 个答案:

没有答案
相关问题