MVC - DropDown containing all distinct values for one field in database?

﹥>﹥吖頭↗ 提交于 2019-12-22 01:08:51

问题


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:


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.



来源:https://stackoverflow.com/questions/16993777/mvc-dropdown-containing-all-distinct-values-for-one-field-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!