How to get multiselected Dropdownlist values ​​in asp.net mvc

I have a problem to get multi select combobox values. Can someone suggest me how to get some dropdownlist values ​​as well as how to get them in the controller.

My code looks 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 a single value is selected and can save it in DB.But, how to select multiple values, and also get them in the controller to save them.

Note. - I am extracting dropdownlist values ​​from DB as shown above.

View

 @Html.ListBoxFor(x => Model.BusinessUnitSiteSafetyRepresentative,new MultiSelectList((IEnumerable<SelectListItem>)@Viewbag.list1) 

I went through several examples, but none of them helped me. Please help me.

+6
source share
3 answers

What I'm suggesting is that your model should be one-to-many with the items in your picklist.

An example is a blog with several tags:

Your blog model might look like this:

 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 looks like this:

  public int TagID{ get; set; } public string TagName{ get; set; } public virtual ICollection<Blog> Blogs{ get; set; } 

Now I recommend using the 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 ViewModel);

 @Html.ListBoxFor(m => m.SelectedTags, new MultiSelectList(Model.Tags, "TagID", "Tag") , null) 

The JQuery Chosen plugin is great for http://harvesthq.imtqy.com/chosen/ . You can use it:

 @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. In addition, this will work on your form to create a new blog post and to edit an existing post (add or remove tags).

edit:

On your blog, Creating a controller action, you should fill this out 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(); } 
+26
source

Try changing your modelproperty to a list type to accept a few values:

 public IEnumerable<string> BusinessUnitSiteSafetyRepresentative { get; set; } 
+4
source

Good answer EvoNet. It was a different approach, but it worked well for me.

Here is the official Microsoft way to do this: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp -net-mvc-application

Search: However, a connection table is required in the database, as shown in the following database diagram:

I tried this and yes, he created the table, but I had to start editing the controller to make it write to the table. Then I also had to think about creating cases where relationships already exist, etc.

So, I redid this method, which worked perfectly for me.

+1
source

Source: https://habr.com/ru/post/946942/


All Articles