繁体   English   中英

装订清单 <string> 从模型到视图模型

[英]Binding List<string> from model to viewModel

我正在尝试将List从模型绑定到viewModel

这是我的模型:

public class Instruction
{
    public int ID { get; set; }
    public List<string> Target { get; set; }
    public bool Delete { get; set; }
    public List<string> DeleteLines { get; set; }
    public string SpecificLine { get; set; }
    public Insert InsertType { get; set; }
    public string Insert { get; set; }
    public MigrationPart? Part { get; set; }
}

视图模型是相同的。 我试图这样绑定它:

        var instructions = myDbContext.Instructions;
        var model = instructions.Select(i => new InstructionView
        {
            Target = i.Target,
            Delete = i.Delete,
            DeleteLines = i.DeleteLines,
            SpecificLine = i.SpecificLine,
            InsertType = i.InsertType,
            Insert = i.Insert,
            Part = i.Part
        });

        return View("MigrationsList", model);

但是,当我尝试显示它时,出现错误:

The specified type member 'Target' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

什么是正确的方法?

首先实现查询:

var instructions = myDbContext.Instructions.ToList();

实际上, ToList将所有数据库记录带入内存。 之后,您将在Entity和ViewModel之间进行映射。

如果不添加ToList ,则Select实际上试图转换为SQL。 由于Entity Framework生成的SQL查询无法获取整数列表并将其映射到您的类中,因此将抛出该异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM