我们如何显示arraylist到dropdownlistfor的每个值? [英] how can we show arraylist each value to dropdownlistfor?

查看:58
本文介绍了我们如何显示arraylist到dropdownlistfor的每个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想做的是:

Actually what I want to do is:

@model Client.AddData

@for( int i=0 ;i <model.address.count;++i)>
{
  @html.dropdownlistfor(model=>model.Address[i],(Ienurable<selectlistitem>)ViewBag.Data)
}


当我这样做时,下拉列表不会显示当前值的文本
即我想在model.address [i] = 2时显示,然后它必须在Dropdown中显示Temporary
我该怎么做,请帮我!!!!!

在Advance中感谢


When I do this the dropdownlist doesnot show the text of current value
i.e i want to show when model.address[i]= 2 ,then it has to show Temporary in Dropdown
how can i do this plz help me !!!!!

Thanks in Advance

推荐答案

假设您的Address 类包含ID ,City ...属性,下面的代码将向您说明如何轻松绑定DropDownList 在Asp.NET MVC应用程序中.

1.在Model 文件夹中,定义地址"和地址集合"类,
Assuming that your Address class contains ID ,City ... attribute, the code below will explain to you how to bind DropDownList easily in Asp.NET MVC application.

1. In Model folder define the Address and Address Collection class,
public class Address{
    public int ID{get;set};
    public string City{get;set};
    // ...
}

public class Addresses:List<Address> {
    public Addresses() {
        this.Add(new Address() {ID=1, City="A"});
        this.Add(new Address() {ID=2, City="B"});
    }
}


2在Contoller 类中,将使用以下地址集合.


2 In Contoller class that is going to consume the Address collection as follow.

public ActionResult Index()
{
    Addresses addresses = new Addresses();
    ViewBag.Addresses= new SelectList(addresses.AsEnumerable(), "ID","City");
    return View();
}


3.在View 页面中,使用ViewBag.Addresses值,如下所示.


3. In the View page consume the ViewBag.Addresses values as follow.

@Html.DropDownList("address", (SelectList)ViewBag.Addresses, "All Addresses")


这篇关于我们如何显示arraylist到dropdownlistfor的每个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆