Why is CascadeFrom() not doing anything?

坚强是说给别人听的谎言 提交于 2020-01-17 03:25:17

问题


I'm really new to Kendo UI, and I'm having problems with CascadeFrom() not calling an action on my controller. Here's the bare bones of my problem:

// The parent dropdown
<select id="Testing">
    <option value="0">Vehicle</option>
    <option value="1">Driver</option>
    <option value="2">Trailer</option>
</select>

// The dynamic dropdown                                      
@(Html.Kendo().DropDownListFor(m => m.VDTId)
    .DataValueField("Id")
    .DataTextField("Item")
    .DataSource(ds =>
    {
        ds.Read(c => c.Action("GetVDT", "CompanyVDTUnavailability")
            .Data("getVDTSelection"))
            .ServerFiltering(true);
    })
    .CascadeFrom("Testing"))

// Function to allow Kendo to pass a value to 
// the type parameter of my GetVDT action.
function getVDTSelection() {
    return {
        type: parseInt($("#Testing").val())
    };
}

The action is being called when the page first loads, and returns the correct data. The problem is, if I then make a selection from the Testing dropdown, the action is never invoked on the controller (I've verified this using a breakpoint on the action), meaning the dynamic dropdown never gets updated.

Looking through the official example, and other questions around SO, I can't see what I'm doing wrong. Could someone point me in the right direction, please?

Edit: I've tried Petur's solution below by changing the parent dropdown to the following:

@(Html.Kendo().DropDownListFor(m => m.Type)
    .Name("Testing")
    .DataValueField("Id")
    .DataTextField("Text")
    .BindTo(Model.UnavailabilityTypes))

This binds the parent dropdown correctly, but no longer invokes the controller action for the cascading dropdown even when the page first loads. Any suggestions?

Controller action signature as requested:

public JsonResult GetVDT(CompanyUnavailabilityType type)

Where CompanyUnavailabilityType is an enum.


回答1:


Kendo DropDownList can cascade only from another Kendo DropDownList/ComboBox. Turn the first widget into kendo DropDownList and it should start working properly.




回答2:


I think the problem is that getVDTSelection() is returning an int or string value not an Enum value. Change your method sig to an int if not, try a string and the method described in my comment

public JsonResult GetVDT(int type)
{
   //AllowGet might be needed as well
   return Json(jsonObjx,JsonRequestBehavior.AllowGet);
}

Edit: You can also try to Manually force the ddl to cascade. Ditch CascadeFrom and do it manually.

function OnChangeOfParentDDL(e){

    var parentValue =  $("#ParentDDL").val();

    $("#ChildDDL").val("").data("kendoDropDownList").text("");//clear it out

    var child = $("#ChildDDL").data("kendoDropDownList");
    child.dataSource.read({ type : parentValue });    
}



回答3:


Both Petur and C Sharper were on the right track with the problem.

  1. I did need to build the dropdown using Html.Kendo.DropDownList() (I've just verified this after getting the solution to work.)
  2. The method signature on the controller was a problem, but only because I'd had old testing methods left on there, leading to an ambiguous call.

The major difficulty for me was that nothing was being reported in the debugger, so diagnosing the problem was a pain. In the end, I used the Network tab of the Firefox web developer tools to ensure the Ajax request was indeed being sent, and it was. Inspecting that request showed it was leading to an ambiguous call on the controller.

Also, to clear up comments from C Sharper's answer:

  1. The parseInt() call is not required.
  2. The call will correctly map to an enum on the server-side, meaning the method signature I posted in my question is correct.


来源:https://stackoverflow.com/questions/24310479/why-is-cascadefrom-not-doing-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!