c# - 使用 jQuery 驱动的双向数据绑定(bind) "Chosen"下拉

标签 c# jquery .net asp.net-mvc-4 jquery-chosen

好吧,我有一个 jQuery 版本的 Chosen 应用到一个 select 正确显示在我的页面上,我已经用下面的代码完成了。首先,我有一个 BaseController,它设置了一个列出所有可能类别的 ViewBag 属性:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    try
    {
        _connection.Open();
        this.ViewBag.AvailableCategories = new MultiSelectList(_connection.Query<Category>("select * from Category"), "CategoryID", "Name");
    }
    catch (Exception ex)
    {
        throw new HttpException(500, ex.Message);
    }
    finally
    {
        _connection.Close();
    }

    base.OnActionExecuted(filterContext);
}

接下来,当导航到 /Event/View/1 时,我有 EventController 设置(注意此 Controller 基于上述 Controller )以下 View 方法。

public ActionResult View(int id)
{
    Event evt = null;

    try
    {
        evt = Event.Where(_connection, id);
        if (evt == null)
        {
            throw new HttpException(404, "Oops! The event you're looking for does not exist.");
        }
    }
    catch (Exception ex)
    {
        throw new HttpException(500, ex.Message);
    }

    return View(evt);
}

如您所见,它将模型设置为以下模型。

public class Event
{
    public int EventID { get; set; }
    public int BusinessID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int NeighborhoodID { get; set; }

    public IEnumerable<int> CategoryIds
    {
        get
        {
            if (Categories == null) { return new List<int>(); }
            return Categories.Select(c => c.CategoryID).AsEnumerable();
        }
    }

    public List<EventCategory> Categories { get; set; }
    public List<EventHours> Hours { get; set; }

    public static Event Where(IDbConnection connection, int id)
    {
        Event result = null;

        try
        {
            connection.Open();

            var sql =
                @"  select * from Event where EventID = @EventID
                    select * from EventCategory where EventID = @EventID
                    select * from EventHours where EventID = @EventID";
            using (var multiResult = connection.QueryMultiple(sql, new { EventID = id }))
            {
                result = multiResult.Read<Event>().FirstOrDefault();
                if (result != null)
                {
                    result.Categories = multiResult.Read<EventCategory>().ToList();
                    result.Hours = multiResult.Read<EventHours>().ToList();
                }
            }
        }
        finally
        {
            connection.Close();
        }

        return result;
    }
}

最后,我在 View 中有了以下标记。

@Html.DropDownListFor(m => m.CategoryIds,
    ViewBag.AvailableCategories as MultiSelectList,
    new { multiple = "multiple", @class = "chzn-container" })

现在,正如我一开始所说,它可以正确显示并按预期列出所有类别。然而,即使 CategoryIds 属性实际上列出了两个选定的类别,它并没有将它们设置(或为此显示它们)作为加载时选择的下拉列表中的选定类别。

我不得不进一步假设,如果用户从“选择”下拉列表中选择了一个值,那么它也不会在发布时正确绑定(bind),因为它不会在选择项目时更改 HTML。

所以,我的问题是,如何正确地将双向数据绑定(bind)到 MVC 中的 Chosen 下拉列表?

最佳答案

ListBoxFor 是您应该用于多选列表的助手

替换

@Html.DropDownListFor(m => m.CategoryIds,
    ViewBag.AvailableCategories as MultiSelectList,
    new { multiple = "multiple", @class = "chzn-container" })

@Html.ListBoxFor(m => m.CategoryIds,
    ViewBag.AvailableCategories as MultiSelectList,
    new { multiple = "multiple", @class = "chzn-container" })

应该可以解决问题

关于c# - 使用 jQuery 驱动的双向数据绑定(bind) "Chosen"下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12926676/

相关文章:

通过 ajax 加载的 Jquery Accordion 不工作

c# - 使用 Generics C# 3.5 时出现编译错误

.net - 以编程方式添加成员(member)提供者

c# - 如何获取带换行符的textarea文本?

c# - 如何在 Windows 通用应用程序 (Windows 10) 中的 markdowntextblock 中格式化聊天对话

c# - 如何在整个应用程序中设置通用日期格式 - Silverlight/Windows Phone 7

c# - Crystal Reports 和 .NET 4 - 无法加载文件或程序集(使用 LegacyV2RuntimeActivationPolicy ="false")crdb_adoplus.dll

c# - 所有测试私有(private)方法的方法似乎都失败了 (c#)

javascript - 在计算机上使用时,Jquery 中的单击功能无法正常运行

jquery - 获取上一个选择的下拉项