0

我是 MonoRail 的新手,我想弄清楚如何拥有它,以便我可以在下拉列表中选择一个父类别,然后让它显示第二个下拉列表,其中包含父类别的子类别。

如果我使用的是我习惯的 ASP.NET MVC,我将有一个 javascript 函数,该函数将被称为第一个下拉列表的 onchange,并对控制器方法进行 ajax 调用(传入选定的父类别 ID)这将获取该父类别的所有子类别并以 JSON 格式返回它们。然后在回调 javascript 函数中,我将评估 JSON 并使用子类别填充第二个下拉列表。

我将如何使用 MonoRail/jQuery 做到这一点?这是我到目前为止的代码:

$FormHelper.Select("business.category.id", $categories, "%{value='id', text='name', firstoption='Select a Category'}")

$FormHelper.Select("business.category.id", $childCategories, "%{value='id', text='name', firstoption='Select a Sub-Category'}")

然后在 BusinessController.cs 中:

private void AddDataToModels()
        {
            PropertyBag["categories"] = CategoryRepository.GetParentCategories();
            PropertyBag["childCategories"] = CategoryRepository.GetChildCategories(1);
}

感谢您提供有关如何解决此问题的任何意见!

贾斯汀

4

2 回答 2

0

看看这个是否有帮助:

http://ayende.com/Blog/archive/2007/10/08/Cascading-Drop-Downs-in-MonoRail.aspx

于 2010-05-27T20:43:37.867 回答
0

这是其他希望从 jQuery 调用控制器操作并返回 JSON 的人的答案...

控制器方法:

[return: JSONReturnBinder(Properties = "Id,Name")]
        public BusinessType[] GetChildBusinessTypes(int parentId)
        {
            var businessTypes = BusinessTypeRepository.GetChildBusinessTypes(parentId);
            return businessTypes;
        }

Javascript:

$(document).ready(function () {
        $('#business_parentbusinesstype_id').change(function () {
            jQuery.ajax({
                url: "$UrlHelper.For("%{action='$business.site.id/GetChildBusinessTypes'}")",
                data: { parentId: $('#business_parentbusinesstype_id').val() },
                dataType: 'json',
                type: 'GET',
                success: fillChildBusinessTypes,
                error: ajaxError
            });
        });
    });

    function fillChildBusinessTypes(json) {
        //get business types.
        var businessTypes = eval(json);
        //bind business types to dropdown.
        $("#business_businesstype_id").get(0).options.length = 0;
        $("#business_businesstype_id").get(0).options[0] = new Option("Select a Business Type", "0");
        jQuery.each(businessTypes, function(index, item) {
            $('#business_businesstype_id').get(0).options[$("#business_businesstype_id").get(0).options.length] = new Option(item.Name, item.Id);
        });
        //show child dropdown.
        Show($('#spnChildBusinessTypes'));
    }
于 2010-05-28T14:37:40.597 回答