MultiSelectList并不突出previously所选项目 [英] MultiSelectList does not highlight previously selected items

查看:101
本文介绍了MultiSelectList并不突出previously所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ASP.NET MVC(剃刀)项目中,我使用的是的ListBox 在一个编辑查看多选选项,

In a ASP.NET MVC (Razor) project, I'm using a ListBox with Multi Select option in a Edit View,

控制器

    public ActionResult Edit(int id)
    {
        Post post = db.Posts.Find(id);
        string selectedValues = post.Tags; //This contains Selected values list (Eg: "AA,BB")
        ViewBag.Tagslist = GetTags(selectedValues.Split(','));
        return View(post);
    }

    private MultiSelectList GetTags(string[] selectedValues)
    {
        var tagsQuery = from d in db.Tags
                        orderby d.Name
                        select d;
        return new MultiSelectList(tagsQuery, "Name", "Name", selectedValues);

    }

HTML

    <div class="editor-field">
        @Html.ListBox("Tags", ViewBag.Tagslist as MultiSelectList)
    </div>

这可加载项(标签列表),以的ListBox ,但并不突出在选定的值项目列表。

This loads the items (Tag List) in to ListBox, but does not highlight the items in the Selected Values list.

如何解决这个问题?

先谢谢了。

推荐答案

我怀疑你的发表类(对此您的看法是强类型的)有一个叫做物业标签。您还可以使用标签 ListBox中的第一个参数帮手。这意味着,助手将考虑该物业第一和不理你传递给 MultiSelectList 选定值。因此,要设置所选值的正确方法如下:

I suspect that your Post class (to which your view is strongly typed) has a property called Tags. You also use Tags as first argument of the ListBox helper. This means that the helper will look into this property first and ignore the selected values you passed to the MultiSelectList. So the correct way to set a selected value is the following:

public ActionResult Edit(int id)
{
    Post post = db.Posts.Find(id);
    ViewBag.Tagslist = GetTags();
    return View(post);
}

private MultiSelectList GetTags()
{
    var tagsQuery = from d in db.Tags
                    orderby d.Name
                    select d;
    return new MultiSelectList(tagsQuery, "Name", "Name");

}

和视图:

<div class="editor-field">
    @Html.ListBoxFor(x => x.Tags, ViewBag.Tagslist as MultiSelectList)
</div>


而这里应该说明一个完整的例子:


And here's a full example that should illustrate:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var post = new Post
        {
            Tags = new[] { "AA", "BB" }
        };
        var allTags = new[]
        {
            new { Name = "AA" }, new { Name = "BB" }, new { Name = "CC" },
        };
        ViewBag.Tagslist = new MultiSelectList(allTags, "Name", "Name");
        return View(post);
    }
}

另外我想用视图模型,而不是通过你的域实体视图推荐你。因此,在你的 PostViewModel 您将有一个名为属性 AllTags 类型 MultiSelectList 。这样,你就可以摆脱弱类型化 ViewBag

Also I would recommend you using view models instead of passing your domain entities to the view. So in your PostViewModel you will have a property called AllTags of type MultiSelectList. This way you will be able to get rid of the weak typed ViewBag.

这篇关于MultiSelectList并不突出previously所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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