本文介绍了MVC - DropDown包含数据库中一个字段的所有不同值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在我的视图中使用 Model 来创建一个下拉列表,其中包含我的数据库中的一列(字段)的所有不同值。



我实际上不确定通常调用 Model 是什么,但显然它不是ViewModel。这是模型我指的是:



@model IEnumerable< Model。学生>



我需要的字段是公司。我是MVC的新手,不确定从哪里开始,因此所有建议都受到赞赏。



我也一直在寻找其他问题,但找不到解决问题的方法我的问题。



编辑:我使用C#编写ASP.Net 4.5并使用Razor视图。 b

解决方案

我最终得到了这个结果: select id =companyname =company>
@foreach(Model.Select中的var c(s => s.Company).Distinct())
{
< option id =c- @ count> @ ℃下/选项>
}
< / select>

已经过测试。它完全符合我的需求。也许这会帮助某人,也许不会。这看起来不太传统。

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.

解决方案

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.

这篇关于MVC - DropDown包含数据库中一个字段的所有不同值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 03:39