New with select2 - select2 don't work with @Html.DropDownList - MVC ?

439 views
Skip to first unread message

Ahmed Amen

unread,
Jun 30, 2015, 2:31:05 PM6/30/15
to sel...@googlegroups.com
Hi
i try to use select2 in my project - but it's not working ? - i got result as:

is use code as:

<div class="form-group">
     @Html.DropDownList("GovernmentID", null, htmlAttributes: new { @class = "select2-hidden-accessible", data_placeholder = "إختر المحافظة", @tabindex = "-1", @style = "width=100%" })
</div>

and get data throw MVC Controller as:

        public ActionResult Create()
       
{
           
ViewBag.GovernmentID = new SelectList(db.Governments, "GovernmentID", "GovernmentName");
           
return View();
       
}

but i got result as image below - i try other code and it works:


                                    <div class="form-group">
                                       
<select id="GovernmentID" style="width: 100%" data-placeholder="إختر المحافظة" tabindex="-1" class="select2-hidden-accessible">
                                           
<option></option>   <!-- empty, for placeholder -->
                                           
<option value="1">Adam</option>
                                           
<option value="2">Amalie</option>
                                           
<option value="3">Nicolas</option>
                                           
<option value="4">Wladimir</option>
                                           
<option value="5">Johnson</option>
                                           
<option value="6">Samantha</option>
                                           
<option value="7">Estefania</option>
                                           
<option value="8">Natasha</option>
                                           
<option value="9">Nicole</option>
                                           
<option value="10">Adrian</option>
                                       
</select>
                                   
</div>

so please how can i make it works on MVC as it works on html ?
Message has been deleted

ch.sa...@gmail.com

unread,
Jul 5, 2015, 3:44:11 PM7/5/15
to sel...@googlegroups.com
Use ListBoxFor instead.

@Html.ListBoxFor(model => model.YourModel, new SelectList(Model.YourModel, "Id", "Name"))

Reply all
Reply to author
Forward
0 new messages