简体   繁体   中英

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

I want to allow the user to add as many image URL's as they want.

So I made a class in Asp.Net Core razor pages Web App. Here is my current Code:

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

When I try to create the actual razor pages using scafolding and selecting this model as the model of the page, it gives me this error: Error-Image

why this happens? and what should i do to fix it?

If each product can have multiple images, you have a one-to-many relationship and the image paths needs their own table. You should create an new class:

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

Then the ImagePath property will look like this:

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

However, if each product only has one image, your property shouldn't be a collection:

public string ImagePath {get;set;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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