在HTML.DropDownList C#MVC中循环浏览列表
我一直在尝试循环浏览下拉列表中的列表,但它似乎不起作用。这是密码在HTML.DropDownList C#MVC中循环浏览列表,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我一直在尝试循环浏览下拉列表中的列表,但它似乎不起作用。这是密码 public class AnimalHandler { public List<string> DogBreeds = new List<string>() { "Affenpinscher","Lhasa Apso","Shitzu", "Tibetan Terrier" } } 公共类AnimalHandler { 公共列表狗种=新列表() { “阿芬平舍”、“
public class AnimalHandler
{
public List<string> DogBreeds = new List<string>()
{
"Affenpinscher","Lhasa Apso","Shitzu", "Tibetan Terrier"
}
}
公共类AnimalHandler
{
公共列表狗种=新列表()
{
“阿芬平舍”、“拉萨阿索”、“狮子”、“藏梗”
}
}
然后在视图中
Project1.Models.AnimalHandler animal = new Project1.Models.AnimalHandler();
@Html.DropDownList("breed", new List<SelectListItem> {
foreach(var item in animal.DogBreeds)
{
new SelectListItem {Text="item", Value=""},
}
new SelectListItem {Text="Choose a Breed", Value=""}
})
Project1.Models.AnimalHandler animal=newproject1.Models.AnimalHandler();
@Html.DropDownList(“品种”,新列表{
foreach(动物品种中的变量项)
{
新建SelectListItem{Text=“item”,Value=”“},
}
新建SelectListItem{Text=“选择品种”,Value=”“}
})
我的想法是,var项将循环遍历dogbrides中的所有项,但是似乎有一个错误,我不知道它可能是什么
也许还有另一种方法可以做到这一点?谢谢诸如此类的东西:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds), "Choose a breed")
从“注释”中,设置选定值:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds,
"Great Dane"),
"Choose a breed")
从何处获取所选值取决于您。类似于以下内容:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds), "Choose a breed")
从“注释”中,设置选定值:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds,
"Great Dane"),
"Choose a breed")
从何处获取所选值取决于您。类似于以下内容:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds), "Choose a breed")
从“注释”中,设置选定值:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds,
"Great Dane"),
"Choose a breed")
从何处获取所选值取决于您。类似于以下内容:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds), "Choose a breed")
从“注释”中,设置选定值:
@Html.DropDownList("breed", new SelectList(animal.DogBreeds,
"Great Dane"),
"Choose a breed")
您从何处获取所选值取决于您。以下内容如何:
@Html.DropDownList(
"breed",
animal.DogBreeds.Select(x => new SelectListItem { Text = x, Value = x }),
"Choose a Breed")
比如说:
@Html.DropDownList(
"breed",
animal.DogBreeds.Select(x => new SelectListItem { Text = x, Value = x }),
"Choose a Breed")
比如说:
@Html.DropDownList(
"breed",
animal.DogBreeds.Select(x => new SelectListItem { Text = x, Value = x }),
"Choose a Breed")
比如说:
@Html.DropDownList(
"breed",
animal.DogBreeds.Select(x => new SelectListItem { Text = x, Value = x }),
"Choose a Breed")
另外,你没有在你的例子中说明AnimalHandler中的代码是如何与你的Animal类链接的。很抱歉我误解了你:)+1真的这么简单吗?!太可笑了,哈哈。我能看到的唯一问题是传递的值。是否有方法更改给定的值。我正在规划URL结构,使其看起来像/Dogs/Grey Hound。您没有在示例中说明AnimalHandler中的代码如何与您的Animal类链接。抱歉,我误解了您:)+1它真的这么简单吗?!太可笑了,哈哈。我能看到的唯一问题是传递的值。是否有方法更改给定的值。我正在规划URL结构,使其看起来像/Dogs/Grey Hound。您没有在示例中说明AnimalHandler中的代码如何与您的Animal类链接。抱歉,我误解了您:)+1它真的这么简单吗?!太可笑了,哈哈。我能看到的唯一问题是传递的值。是否有方法更改给定的值。我正在规划URL结构,使其看起来像/Dogs/Grey Hound。您没有在示例中说明AnimalHandler中的代码如何与您的Animal类链接。抱歉,我误解了您:)+1它真的这么简单吗?!太可笑了,哈哈。我能看到的唯一问题是传递的值。是否有方法更改给定的值。例如,我正在规划URL结构,使其看起来像/Dogs/Grey Hound。