c# - asp.net mvc ListBoxFor - MultiSelectList does not apply the selected item -


the tags multi-selectable , retrieved database.

this action in controller :

public actionresult editdocument(long documentid, string returnurl)     {         viewbag.returnurl = returnurl;          int userid = accountcontroller.getloggedinid(session);         string pageurl = "document/editdocument";          if (isdocumentaccessible(documentid,pageurl) && checkoperation("editdocument",pageurl))         {             try             {                  spgetdocumentbyid_result found = db.spgetdocumentbyid(documentid).toarray()[0];                  editdocumentviewmodel model = new editdocumentviewmodel()                 {                     documentid = found.documentid,                     documentnumber = found.documentnumber,                     documentdate = found.documentdate,                     documentleveltype = (int)found.documentleveltypeid,                     addresssrc = found.address                 };                  list<spgettagsofdocument_result> tags = db.spgettagsofdocument(documentid).tolist();                  foreach (spgettagsofdocument_result tag in tags)                 {                     model.selectedtags.add(tag.tagname);                 }                  list<spgetalldocumentleveltype_result> documentleveltypelist = db.spgetalldocumentleveltype(userid, pageurl).tolist();                 viewbag.documentleveltypeid = new selectlist(documentleveltypelist, "documentleveltypeid", "documentleveltypename");                  list<spgetalltags_result> taglist = db.spgetalltags(accountcontroller.getloggedinid(session), "document/adddocument").tolist();                 viewbag.tags = new multiselectlist(taglist, "tagid", "tagname", model.selectedtags.toarray());                  return view(model);             }             catch (exception)             {                 return new httpstatuscoderesult(httpstatuscode.badrequest);             }         }          return new httpstatuscoderesult(httpstatuscode.forbidden);     } 

and view:

<div class="form-group">                             @html.labelfor(model => model.tags, new { @class = "col-sm-3 control-label" })                             <div class="col-sm-8">                                 @html.listboxfor(model => model.tags, viewbag.tags multiselectlist, new { @class = "form-control" })                             </div>                         </div> 

the thing selected item not apply! multi-select element filled fine there no preselection!

have tried passing id instead of name selected tags:

foreach (spgettagsofdocument_result tag in tags) {     model.selectedtags.add(tag.tagid); } 

it can problem name of listbox being same multiselectlist, can try change name of tag list passing:

 viewbag.taglist = new multiselectlist(taglist, "tagid", "tagname", model.selectedtags.toarray()); 

and pass list:

@html.listboxfor(model => model.tags, viewbag.taglist multiselectlist, new { @class = "form-control" }) 

Comments