This is a migrated thread and some comments may be shown as answers.

Cascading error

1 Answer 49 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Eduardo
Top achievements
Rank 1
Eduardo asked on 02 Mar 2012, 05:15 PM
Hi, I've three comboboxes in cascade, and the three are properties of a ViewModel:
public class MyOrder
    {
        public int Category { getset; }
 
        public int Product { getset; }
 
        public int Orders { getset; }
    }

The View:
@(
    Html.Telerik().ComboBoxFor(model => model.Category)
        .DataBinding(binding => binding.Ajax().Select("_SelectCategory""CascadingComboBox"))
        .ClientEvents(e => e.OnLoad("onLoadCombo").OnChange("onChangeCombo"))
        .CascadeTo("Product")
)
<br />
@(
    Html.Telerik().ComboBoxFor(model => model.Product)
        .DataBinding(binding => binding.Ajax().Select("_SelectProduct""CascadingComboBox"))
        .CascadeTo("Orders")
        .ClientEvents(e => e.OnLoad("onLoadCombo").OnChange("onChangeCombo"))
)
<br />
@(
    Html.Telerik().ComboBoxFor(model => model.Orders)
        .DataBinding(binding => binding.Ajax().Select("_SelectOrders""CascadingComboBox"))
        .ClientEvents(e => e.OnLoad("onLoadCombo"))
)

Using the OnLoad event, the comboboxes get the values:
function onLoadCombo(e) {
        $(e.target).data('tComboBox').reload();
        $(e.target).data('tComboBox').enable();
    }

The controller:
public class CascadingComboBoxController : Controller
    {
        readonly Entities context = new Entities();
 
        public ActionResult Index()
        {
            return View(new MyOrder {Product = 24, Category = 1, Orders = 10352});
        }
 
        public JsonResult _SelectCategory()
        {
            return Json(new SelectList(context.Categories, "CategoryID""CategoryName"), JsonRequestBehavior.AllowGet);
        }
 
        public JsonResult _SelectProduct(int? Category)
        {
            return
                Json(
                    new SelectList(context.Products.Where(cat => !Category.HasValue || cat.CategoryID == Category),
                                   "ProductID""ProductName"), JsonRequestBehavior.AllowGet);
        }
 
        public JsonResult _SelectOrders(int? Product)
        {
            return
                Json(
                    new SelectList(
                        context.Order_Details.Where(order => !Product.HasValue || order.ProductID == Product), "OrderID",
                        "OrderID"), JsonRequestBehavior.AllowGet);
        }
    }


The problem is when I change the parent combo, the cascadeTo combo set it's text to the value of the property, and I was trying to chage this behavior in the OnChange event of the parent, but doesn't work. If you have a solution to this, it would be great.

Eduardo

1 Answer, 1 is accepted

Sort by
0
rudy
Top achievements
Rank 1
answered on 03 Apr 2013, 04:16 PM
I implemented a workaround in my project:

function onCmbDataBound(e) {

// fix issue on cascading box displaying the current bound ID in box when cascade occurs

var txt = $(e.target).data("tComboBox").text();

var testPattern =/\d$/;

if (testPattern.test(txt) == true)

$(e.target).data("tComboBox").select(0)

}

in your ComboBoxFor that you cascade to just add this event:

.ClientEvents(x => x.OnDataBound("onCmbDataBound"))

Rudy Hamman

 

Tags
ComboBox
Asked by
Eduardo
Top achievements
Rank 1
Answers by
rudy
Top achievements
Rank 1
Share this question
or