如何在 Asp.Net Core Web App 中向模型的一个字段添加多个值?

How to add multiple values to one field of a model in Asp.Net Core Web App?

我想允许用户添加任意​​数量的图像 URL。

所以我在 Asp.Net Core razor pages Web App 中做了一个 class。这是我当前的代码:

public class Post
{
    public int ID { get; set; }
    public string Text { get; set; }
    public List<string> Url { get; set; }
    public string Date { get; set; }
}

当我尝试使用脚手架创建实际的剃须刀页面并选择此模型作为页面模型时,出现以下错误:Error-Image

为什么会这样?我应该怎么做才能解决它?

如果每个产品可以有多个图像,则您有一对多关系,图像路径需要自己的 table。您应该创建一个新的 class:

public FilePath
{
    public int ID {get;set;}
    public string Path {get;set;}
}

然后 ImagePath 属性 将如下所示:

public List<FilePath> ImagePath {get;set;}

但是,如果每个产品只有一张图片,您的 属性 不应该是一个集合:

public string ImagePath {get;set;}