asp.net-mvc 如何在asp.net mvc 中获取多选的下拉列表值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17021978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:56:04  来源:igfitidea点击:

How to get multiselected Dropdownlist values in asp.net mvc

asp.net-mvcrazor

提问by user2436792

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.

好吧,如果选择单个值,我可以得到并可以保存到 DB。但是如何选择多个值以及将它们放入 Controller 中以保存它们。

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.

我已经经历了一些例子,但没有一个对我有帮助。请帮助我。

回答by Evonet

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);

最后在您的视图中(从 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:

JQuery Chosen 插件非常适合这个http://harvesthq.github.io/chosen/。您可以通过以下方式使用它:

@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();
    }

回答by Jan

Try to change your modelproperty to a list type to accept multiple values:

尝试将您的模型属性更改为列表类型以接受多个值:

public IEnumerable<string> BusinessUnitSiteSafetyRepresentative { get; set; }

回答by ozzy432836

Good answer by EvoNet. Its a different approach but worked well for me.

EvoNet 的回答很好。这是一种不同的方法,但对我来说效果很好。

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

这是微软官方的做法: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 for: A join table is required in the database, however, as shown in the following database diagram:

搜索:数据库中需要连接表,但是如下数据库图所示:

I tried it and yes it created the table but I had to start editing the controller to get it to write to the table. Then I also had to think about creating cases for when a relationship already exists etc.

我试过了,是的,它创建了表格,但我必须开始编辑控制器才能让它写入表格。然后我还必须考虑为已经存在关系等的情况创建案例。

So I reveted to this method which worked just fine for me.

所以我重新使用了这种对我来说效果很好的方法。