如何在编辑模式下选择所选项目在MVC C中多选下拉列表#

时间:2015-11-19 07:13:01

标签: c# asp.net-mvc-4

我想在编辑模式中选择我选择的项目。我正在使用多选下拉列表:

@Html.DropDownListFor(m => Model.BRAND_ID, Model.BRAND_List, null,
      new { @class = "inputbox", multiple = "multiple",@style="height:120px;"})

当我在编辑模式下打开表单时,它总是突出显示单个项目而不是多个我的值是来自DB的多个。

这是我到目前为止所尝试的:

public List<Brands> BrandsList() {
    DataSet ds;
    DataTable dt;
    List<Brands> BrandsList = new List<Brands>();

    ds = Cls_EM_Admin_BRANDS.getBrands();
    dt = ds.Tables[0];
    //BrandsList.Add(new Brands { Brand_ID = 0, Brand_Name ="Select"});
    int i = 0;

    foreach (DataRow item in dt.Rows) {
        BrandsList.Add(new Brands { 
            Brand_ID = Convert.ToInt32(item["ID"]),
            Brand_Name = item["Title"].ToString()
        });
    }
    return BrandsList;
}

if (ID != null) {  ///this condition runs on edit
    var objModel_Brands = new EM_BrandAdmin_Model()
        .Get_BrandAdmin_DATA(Convert.ToInt32(ID));
    BrandsSelect_List = new SelectList(
    BrandsList(), "Brand_ID", "Brand_Name", objModel_Brands.BrandID);
    objModel_Brands.BRAND_List = BrandsSelect_List;

    return view(objModel_Brands)
}

public EM_BrandAdmin_Model Get_BrandAdmin_DATA(Int32 P_ADMIN_ID) {
    try {
        objModel_BrandAdmin = new EM_BrandAdmin_Model();
        cmd = new OracleCommand("SP_BRAND_ADMIN_LIST", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Prepare();
        cmd.Parameters.Add(new OracleParameter("P_ADMIN_ID", OracleDbType.Int32))
            .Value = P_ADMIN_ID;

        cn.Open();
        dr = cmd.ExecuteReader();
        while (dr.Read()) {
            objModel_BrandAdmin = new EM_BrandAdmin_Model();
            objModel_BrandAdmin.ADMIN_ID = Convert.ToInt32(dr["ADMIN_ID".ToUpper()]);
            objModel_BrandAdmin.BRAND_ID = Convert.ToInt32(dr["BRAND_ID"]);
        }
    }
    catch (Exception ex) {
        throw ex;
    }
    finally {
        cn.Close();
        if (dr != null) {
            dr.Close();
        }
    }
    return objModel_BrandAdmin;
}

1 个答案:

答案 0 :(得分:1)

在您的模型中,更改

boxplot(mydata)

public int BRAND_ID { get; set; }
public List<Brands> BRAND_List { get; set; }

在您看来,更改

public List<int> BRAND_ID { get; set; }
public IEnumerable<SelectListItem> BRAND_List { get; set; }

@Html.DropDownListFor(m => Model.BRAND_ID, Model.BRAND_List, null, new { @class = "inputbox", multiple = "multiple",@style="height:120px;"})

在您的品牌课程中,添加

@Html.DropDownListFor(m => m.BRAND_ID, Model.BRAND_List, null, new { @class = "inputbox", multiple = "multiple",@style="height:120px;"})

在您的控制器中,更改

public bool Selected { get; set; }

BrandsList.Add(new Brands { Brand_ID = Convert.ToInt32(item["ID"]), Brand_Name = item["Title"].ToString() });
...
BrandsSelect_List = new SelectList(BrandsList(), "Brand_ID", "Brand_Name", objModel_Brands.BrandID);

这是demo

但是,我建议您使用BrandsList.Add(new Brands { Brand_ID = Convert.ToInt32(item["ID"]), Brand_Name = item["Title"].ToString(), Selected = true }); //Note: replace "true" above with code to retrieve the selection from the db ... BrandsSelect_List = new List<SelectListItem>(); foreach (Brands b in BrandsList()){ BrandsSelect_List.Add(new SelectListItem(){ Value = b.BRAND_ID, Text = b.Brand_Name, Selected = b.Selected } } 代替ListBoxFor,因为它会使您的代码更简单。如果您想使用DropDownListFor,请忽略上述解决方案并执行以下操作:

在您的模型中,更改

ListBoxFor

public int BRAND_ID { get; set; }
public List<Brands> BRAND_List { get; set; }

在您看来,更改

public List<int> BRAND_ID { get; set; }
public IEnumerable<SelectListItem> BRAND_List { get; set; }

@Html.DropDownListFor(m => Model.BRAND_ID, Model.BRAND_List, null, new { @class = "inputbox", multiple = "multiple",@style="height:120px;"})

在您的控制器中,更改

@Html.ListBoxFor(m => m.BRAND_ID, Model.BRAND_List, new { @class = "inputbox", @style="height:120px;"})

从数据库中读取整数列表,这个列表就是您的选择。

这是demo