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