0

I have many to many relationship in edit page how can i show selected items selected in a Dropdown multi select

here is my code:

        // GET: Project/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        var edata = db.Projects.Find(id);
        //check if data
        if (edata == null)
            return HttpNotFound();

        //IEnumerable<SelectListItem> items = db.Provinces.Select(c => new SelectListItem { Value = c.ID.ToString(), Text = c.Name });
        //ViewBag.ProvinceId = items;

        //---Get all provice-----
        var Prov = from c in db.Provinces select c;
        //------get province ids---------------
        var prov_id = from o in db.ProRel where o.ProjectId == id select o.ProvinceIds;
        List<int> mid_list = new List<int>();
        foreach (var p_ids in prov_id)
        {
            mid_list.Add(p_ids);
        }
        var option_vals = "";
        var selected = string.Empty;
        foreach (var p_itmes in Prov)
        {
            if (mid_list.Contains(p_itmes.ID))
            {
                selected = "selected='selected'";
            }
            else
            {
                selected = "";
            }
            option_vals += "<option "+selected+" value="+p_itmes.ID+">"+p_itmes.Name+"</option>";
        }
        string test = option_vals.ToString();
        string test2 = test.Replace("\"", "");
        ViewBag.options = test2;
        return View(edata);
    }

in my edit view my code:

      <div class="form-group">
        <label class="control-label col-md-2">Provinces</label>
        <div class="col-md-10">
            <select name="prov" id="prov">
                @ViewBag.options
            </select>
        </div>
    </div>

it displays the option values with double quotes at the end and it does not render like Html select options how can I remove the quotes that my options should be shown in its normal mode.

when I inspect element in browser the options are shown like this:

"<option  value=1>Kabul</option><option selected='selected' value=2>Mazar</option><option selected='selected' value=3>Parwan</option><option  value=4>Herat</option><option  value=5>Badakhshan</option><option  value=6>Takhar</option><option  value=7>Smanagan</option><option selected='selected' value=8>Zabul</option>"

or you suggest me another way for this I am new to ASP.Net

Benafsh Yalda
  • 396
  • 1
  • 5
  • 13

2 Answers2

2

Short answer was already given by Marcos via his comment, but in general thats not how you create a select tag within MVC 5:

Controller:

public ActionResult Edit(int? id)
{
    // other code
    var options = new List<SelectListItem>();
    foreach (var p_itmes in Prov)
    {
        var item = new SelectListItem();
        if (mid_list.Contains(p_itmes.ID))
            item.Selected = true;
        item.Value = p_itmes.ID.ToString();
        item.Text = p_itmes.Name;
        options.Add(item);
    }
    ViewBag.options = options;
    return View(edata);
}

View:

<div class="form-group">
    <label class="control-label col-md-2">Provinces</label>
    <div class="col-md-10">
        @Html.DropDownList("prov", (List<SelectListItem>)ViewBag.options,
                           htmlAttributes: new { multiple = "multiple" })
    </div>
</div>
Christoph Fink
  • 22,099
  • 9
  • 67
  • 105
2

I agree with @ChrFin Answer but with some more details which I hope will solve your problem

     // GET: Project/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        var edata = db.Projects.Find(id);
        //check if data
        if (edata == null)
            return HttpNotFound();

        //IEnumerable<SelectListItem> items = db.Provinces.Select(c => new SelectListItem { Value = c.ID.ToString(), Text = c.Name });
        //ViewBag.ProvinceId = items;

        //---Get all provice-----
        var Prov = from c in db.Provinces select c;
        //------get province ids---------------
        var prov_id = from o in db.ProRel where o.ProjectId == id select o.ProvinceIds;
        List<int> mid_list = new List<int>();
        foreach (var p_ids in prov_id)
        {
            mid_list.Add(p_ids);
        }

        var options = new List<SelectListItem>();
        foreach (var p_itmes in Prov)
        {
            var item = new SelectListItem();
            if (mid_list.Contains(p_itmes.ID))
                item.Selected = true;
            item.Value = p_itmes.ID.ToString();
            item.Text = p_itmes.Name;
            options.Add(item);
        }
        ViewBag.options = options;
        return View(edata);

    }

and inside your view add the following extra attribute:

    <div class="form-group">
        <label class="control-label col-md-2">Provinces</label>
        <div class="col-md-10">
            @Html.DropDownList("prov", (List<SelectListItem>)ViewBag.options, htmlAttributes: new { @class = "form-control", @multiple = "multiple" })
        </div>
    </div>
MJ X
  • 7,476
  • 10
  • 61
  • 88