在asp.net mvc的如何获得multiselected DROPDOWNLIST值 [英] How to get multiselected Dropdownlist values in asp.net mvc

查看:108
本文介绍了在asp.net mvc的如何获得multiselected DROPDOWNLIST值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题让多选择下拉列表values​​.can有人建议我怎么去选择多个DropDownList的值,以及如何在控制器得到它们。

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.

我的code是这样的: -

My code is like this:-

型号

public string BusinessUnitSiteSafetyRepresentative { get; set; }

控制器

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

那么,如果单值选择和放大器,我可以得到;可节省DB.But如何选择多个值,以及在控制器让他们这样才能拯救他们。

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.

请注意: - 我从数据库中检索DropDownList的值,如上图所示。

Note: - I am retrieving the dropdownlist values from DB as shown above.

查看

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

我已经通过一些例子走了,但他们没有帮助me.Please帮助我。

I have gone through some examples but none of them helped me.Please help me.

推荐答案

我的建议是,你的模型需要有一个人跟你多选择列表中的项目一对多的关系。

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)

了jQuery插件选上是极好的这 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)

编辑:

在您的博客创建控制器动作,你会填充此为:

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

这篇关于在asp.net mvc的如何获得multiselected DROPDOWNLIST值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆