Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this 2 models

  public class FileType
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }



public class Genre
    {
        public int Id { get; set; }

        public string Name { get; set; }

        [ForeignKey("FileType")]
        public int FileTypeID { get; set; }
    }

And tables with data in PostgreSQL database for these models, i want to create 2 dropdownlists in view with Name for filetype and name for genre.I want to create that 2 ddl in this upload view

 public class FileUploadViewModel
    {
        public List<FileModel> Files { get; set; }
        public string Name { get; set; }
        public string Extension { get; set; }
        public string Description { get; set; }
        public string Author { get; set; }
        public string Year { get; set; }
        public string FilePath { get; set; }
        public string PublishedOn { get; set; }
        public int DownloadCounter { get; set; }
     
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
149 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you want to create a dropdown list for FileTypeID, you could try this:

(...)
public class Genre
    {
        public int Id { get; set; }

        public string Name { get; set; }

        [ForeignKey("FileType")]
        public FileType FileTypeID { get; set; }
    }

public enum FileType
{
Item1,
Item2,
Item3,
Item4
}

Then, in your view:

@model YourNameSpace.Models.Genre

@{
    ViewData["Title"] ="xyz";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.DropDownListFor(m => m.FileTypeID, new SelectList(Enum.GetValues(typeof(FileType))), "Select FileType", new { @class = "form-control" })

You will then see a dropdown list with the four selectable items item1-item4 and a default placeholder stating "Select FileType".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...