MVC - DropDown containing all distinct values for

2019-04-15 00:02发布

I'm trying to create a dropdown list that contains all distinct values for ONE column (field) from my database by using the Model in my view.

I'm actually not sure what the Model is normally called, but apparently it's NOT a ViewModel. This is the Model I'm referring to:

@model IEnumerable<Model.Student>

The field that I need is Company. I'm new to MVC and not sure where to begin so all suggestions are appreciated.

I've also been searching other questions, but can't find one that solves my problem.

Edit: I'm coding in C#, ASP.Net 4.5 and using Razor views.

1条回答
霸刀☆藐视天下
2楼-- · 2019-04-15 00:38

I ended up with this:

<select id="company" name="company">
    @foreach(var c in Model.Select(s => s.Company).Distinct())
    {
        <option id="c-@count">@c</option>
    }
</select>

Already tested. It does exactly what I need. Maybe this will help someone, maybe not. It doesn't seem very conventional.

查看更多
登录 后发表回答