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

发布于 2024-12-29 04:41:31 字数 3614 浏览 1 评论 0原文

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

databinding.Ajax().Select("action","controller",-->route<<-)

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

提前致谢。

I have two combobox. I need get some value from first combobox1 after combobox1 has changed value and that put this value in Combobox2 route

databinding.Ajax().Select("action","controller",-->route<<-)

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

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-05 04:41:31

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

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

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

new { curentCountry = Request["countryID"] }

您可以查看 文档 说明了如何订阅 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>

You could fetch it from the current RouteData:

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

where countryID is the name of the route parameter you are using. Or if it was part of the query string and not part of your routes:

new { curentCountry = Request["countryID"] }

You may take a look at the documentation which illustrates how you could subscribe to the OnDataBinding event which is raised when a request to fetch data is being made:

So you could subscribe to the OnDataBinding event:

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

and which allows you to pass additional arguments to this request

<script type="text/javascript">
    function onComboBoxDataBinding(e) {
        e.data = $.extend({ }, e.data, { curentCountry: "customValue"});
    }
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文