I have a problem to get multi select dropdown list values.can anyone suggest me how to get select multiple dropdownlist values as well as how to get them in controller.
My code is like this:-
Model
public string BusinessUnitSiteSafetyRepresentative { get; set; }
Controller
[HttpPost]
public ActionResult AddClientBusinessUnitSite(LocalAddClientBusinessUnitSite local)
{
var query = from o in entitydb.systemusersorganizations.toList()
from c in entitydb.contacts.toList()
where o.orgId == clientId
select new SelectListItem
{
Text = c. Name;
Value = c.OrgId.toString()
}
ViewBag.list1 = query.ToList();
}
Well, I can get if single value is selected & can save to DB.But how to select multiple values as well as to get them in Controller so as to save them.
Note: - I am retrieving the dropdownlist values from DB as shown above.
View
@Html.ListBoxFor(x => Model.BusinessUnitSiteSafetyRepresentative,new
MultiSelectList((IEnumerable<SelectListItem>)@Viewbag.list1)
I have gone through some examples but none of them helped me.Please help me.
DropDownListFor(m => m. location_code, Model. location_type, new { @class = "form-control", @multiple = "multiple" }). location_code is an List<int> and location_type is List<SelectListItem> populated with data.
The ASP.NET Core MultiSelect Dropdown is a quick replacement for the HTML select tag for selecting multiple values. HTML MultiSelect Dropdown is a textbox control that allows the user to type or select multiple values from a list of predefined options.
Now I will explain how to get dropdownlist selected value in asp.net mvc with example. Here “DropDownListFor” function will render it as <select> with <options> tags and “m =>m.UserId” is used to retrieve selected value and “Model.usersinfo” will provide all possible options for dropdown.
To implement a Multiple Select (MultiSelect) DropDownList with CheckBoxes in . NET, we will need to make use of checkbox control and apply the jQuery Bootstrap Multi-Select Plugin to it. Provide a Project nam e and confirm or change the Location. Select Create Select the latest version of ASP.NET Core in the drop-down (.
Step 1 : Create your Model. Step 2 : Create your cshtml page. Create a view from your index action method your controller. Right click => add View. Step 3 : Add the below Javascript into your view created at step 2. Here we are getting the value of selected state from dropdown and pass value to a method called "GetCities".
To get the selected value from the dropdown, I am going to add one button and display the selected value on button click event in jQuery. Proceed with the button click event in jQuery, as shown below. Now, run your Application and click button by selecting skills. If you want to get the selected text change in View, write the code, as shown below.
What I suggest is that your model needs to have a one to many relationship with the items in your multi select list.
An example is a Blog with multiple tags:
Your blog model may look like:
public class Blog
{
public Blog()
{
Tags = new List<Tag>();
}
public string BlogTitle{ get; set; }
public string Body{ get; set; }
public virtual ICollection<Tag> Tags{ get; set; }
}
And your tag model like so:
public int TagID{ get; set; }
public string TagName{ get; set; }
public virtual ICollection<Blog> Blogs{ get; set; }
Now I recommend you use a view model:
public class BlogViewModel
{
public Blog blog{ get; set; }
public List<int> SelectedTags { get; set; }
public virtual List<Tag> Tags{ get; set; }
public BlogViewModel()
{
}
public BlogViewModel(Blog _blog, List<Tag> _Tags)
{
blog = _blog;
Tags = _Tags;
SelectedTags = new List<int>();
}
}
And finally in your View (which inherits from the ViewModel);
@Html.ListBoxFor(m => m.SelectedTags,
new MultiSelectList(Model.Tags, "TagID", "Tag")
, null)
The JQuery Chosen plugin is excellent for this http://harvesthq.github.io/chosen/. You can use it by:
@Html.ListBoxFor(m => m.SelectedTags,
new MultiSelectList(Model.Tags, "TagID", "Tag")
, new { @class = "chzn-select", data_placeholder = "Tags..." })
Replace this with your own model and controllers and this should solve your problem. Also, this will work in your form for creating a new blog post, and for editing an existing post (adding and removing tags)
edit:
In your Blog Create controller action, you would populate this as:
public ActionResult Create()
{
var blog = new Blog();
var AllTags = from t in db.Tags
select t;
BlogViewModel viewModel = new BlogViewModel(blog,
Tags.ToList());
return View(viewModel);
}
public ActionResult Create(BlogViewModel blogViewModel)
{
Blog blog = blogViewModel.blog;
if (blogViewModel.SelectedTags != null)
{
foreach (var TagID in blogViewModel.SelectedTags)
{
Tag tag = db.Tags.Where(t => t.TagID == TagID).First();
blog.Tags.Add(tag);
}
}
db.Blog.Add(blog);
db.SaveChanges();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With