I have a control that needs to have multiple select options to create tokens. Whenever the token is created or deleted DB call is generated. Now the problem is when I load a fresh page from the server there is no token in the control. Following is my Razor code:
@Html.DropDownList("ProjectIds", new MultiSelectList(projDDL.Items, "key", "value",Model.SelectedProject), new {@class = "form-control selectorBind", multiple = "multiple",@id= "myid" data_rowid = "ID", data_type="Project",data_myattr= "attr" })
projDDL is a dictionary that contains the dropdown list items
SelectedProject are also dictionary containing selected projects key, value
I have tried to follow this link MVC DropDown list with MultipleSelect but no luck.
Loading tokens from server side is my problem. Though I know I can do it easily from Jquery but I want to use Razor for it.
Solution
Check out @Html.DropDownListFor
vs @Html.ListBoxFor
@Html.ListBoxFor(y => y.SelectedProjects, new MultiSelectList(projDDL.Items, "key", "value", Model.SelectedProjects), new { @class = "form-control selectorBind", multiple = "multiple", @id = @Model.SkillType+"Project_" + Model.ID, data_rowid = @Model.ID, data_type = "Project", data_myattr = "FK_ProjectID" })