如何在客户端为 Telerik 组合框设置路由值

Posted

技术标签:

【中文标题】如何在客户端为 Telerik 组合框设置路由值【英文标题】:How can i set route value in client side for Telerik combobox 【发布时间】:2012-02-17 17:07:14 【问题描述】:

我有两个组合框。在combobox1更改值并将此值放入Combobox2路由后,我需要从第一个combobox1获取一些值

databinding.Ajax().Select("action","controller",-->路由

    @(html.Telerik()
.ComboBoxFor(m => m.Country)
.ClientEvents(e => e.OnChange"onCountryChange"))
.BindTo(Model.ListCountry))

    @(Html.Telerik()
.ComboBoxFor(m => m.UnitOfAdministration)
.ClientEvents(e => e.OnChange("onCityChange"))
.BindTo(Model.ListUnitOfAdministration)
.DataBinding(bind => 
bind.Ajax().Select("GetCityListByStr", "User",new  idCountry = "in this place i need put curent country ID" )
.Delay(1000))

function onCountryChange(e)    
    var fildUnit =  $("#fild_UnitOfAdministration");
    var fildStreet = $("#fild_Street").hide();
    var fildHouse = $("#fild_House").hide();
    var fildSegmentHouse = $("#fild_SegmentHouse").hide();
    var curCountry = Number(e.value);
    if(curCountry.toString() == "NaN" || curCountry==0)
        fildUnit.hide();
    else
    $.post("@(Url.Action("GetCityList", "User"))",  id:curCountry, asd:Math.random() ,
         function (data)                    
                    fildUnit.show();
                    var comboBox = $('#UnitOfAdministration').data('tComboBox');    
        comboBox.dataBind(data);
                    comboBox.select(0);                     
         );



    [HttpPost]
    public ActionResult GetCityList(string id)
    

        int _id = id.ExtractID();
        ViewData["curCountry"] = _id;
        List<SelectListItem> listSel = new List<SelectListItem>();
        listSel.Add(new SelectListItem()  Text = "Виберіть місто", Value = "0", Selected = true );
        TUnitOfAdministration un = TUnitOfAdministration.GetObject(_id);
        if (un != null)
        
            string sql = "lft>" + un.Lft + " AND RGT<" + un.Rgt + " AND TypeUnit in (2,3) order by Name";
            TypedBindingList list = TUnitOfAdministration.GetObjects(sql);
            foreach (TUnitOfAdministration item in list)
            
                listSel.Add(new SelectListItem  Text = item.Name, Value = item.ID.ToString() );
            
        
        return new JsonResult  Data = new SelectList(listSel, "Value", "Text", 0) ;
    
[HttpPost]
    public ActionResult GetCityListByStr(string text,string idCountry) 
    
        text = text.ClearStringFull();
        int _idCountry = idCountry.ExtractID();
        List<SelectListItem> listSel = new List<SelectListItem>();
        TypedBindingList list = new TypedBindingList(typeof(TUnitOfAdministration));
        listSel.Add(new SelectListItem()  Text = "Виберіть місто", Value = "0", Selected = true );
        TUnitOfAdministration country = TUnitOfAdministration.GetObject(_idCountry);
        if (country != null)
        
            string sqlAll = "ID_UnitOfAdministration = " + country.ID_UnitOfAdministration + "  AND Name like '" + text + "%' Order by name";
            list = TUnitOfAdministration.GetObjects(sqlAll);

            //if (list.Count == 0)
            //
            //  string sql = "lft>" + country.Lft + " AND RGT<" + country.Rgt + " AND TypeUnit in (2,3) order by Name";
            //  list = TUnitOfAdministration.GetObjects(sql);
            //

            foreach (TUnitOfAdministration item in list)
            
                listSel.Add(new SelectListItem  Text = item.Name, Value = item.ID.ToString() );
            
        
        return new JsonResult  Data = new SelectList(listSel, "Value", "Text", 0) ;
    

提前致谢。

【问题讨论】:

【参考方案1】:

你可以从当前的 RouteData 中获取它:

new  curentCountry = ViewContext.RouteData.Values["countryID"] 

其中countryID 是您正在使用的路由参数的名称。或者,如果它是查询字符串的一部分,而不是您的路线的一部分:

new  curentCountry = Request["countryID"] 

您可以查看documentation,它说明了如何订阅在请求获取数据时引发的OnDataBinding 事件:

所以你可以订阅OnDataBinding 事件:

@(Html.Telerik()
      .ComboBoxFor(m => m.UnitOfAdministration)
      .ClientEvents(e => e.OnDataBinding("onComboBoxDataBinding"))
      .BindTo(Model.ListUnitOfAdministration)
      .DataBinding(bind => bind.Ajax().Select("GetCityListByStr", "User")
      .Delay(1000)
)

它允许你向这个请求传递额外的参数

<script type="text/javascript">
    function onComboBoxDataBinding(e) 
        e.data = $.extend( , e.data,  curentCountry: "customValue");
    
</script>

【讨论】:

它是客户端...当 combobox1 更改时,combobox2 不知道选择了什么值。我不能把它放在路线中 据我了解.. 我需要使用客户端事件 onBind 并在此函数中创建 ajax 请求 @Sasha,对不起,我误解了你的要求。我已经更新了我的答案。

以上是关于如何在客户端为 Telerik 组合框设置路由值的主要内容,如果未能解决你的问题,请参考以下文章

使用来自javascript的动态ID启用/禁用Telerik asp.net组合框

WPF MVVM Enable/Disable telerik:RadComboBox inside GridViewDataColumn from ViewModel。

将大量数据绑定到组合框?

如何在 Telerik 网格中插入下拉列表框

如何在组合框中将项目设置为选中

从组合框上的多个选择中获取值