Naveed Ahmed
Naveed Ahmed

Reputation: 65

How to Select selected items in edit mode Multiselect Dropdown in MVC C#

I want to Select my selected items in edit mode. I am using multi-select dropdown as follow:

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

No when I open my form in edit mode it always highlight single item not multi my values are multiple from DB.

This is what I tried so far:

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;
}

Upvotes: 1

Views: 3949

Answers (1)

Racil Hilan
Racil Hilan

Reputation: 25351

In your model, change

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

to

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

In your view, change

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

to

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

In your Brands class, add

public bool Selected { get; set; }

In your controller, change

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);

to

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 }
}

Here is a demo.

However, I recommend you to use ListBoxFor instead of DropDownListFor as it will make your code much simpler. If you want to use ListBoxFor then ignore the solution above and do the following:

In your model, change

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

to

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

In your view, change

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

to

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

In your controller, change

objModel_BrandAdmin.BRAND_ID = Convert.ToInt32(dr["BRAND_ID"]);

to read a list of integers from the database, this list is your selection.

Here is a demo.

Upvotes: 1

Related Questions