简体   繁体   English

用组合框重新绑定网格,如何获取选择的值?

[英]Rebind grid with combobox, how to get selected value?

I work with Telerik-MVC and I'm trying to rebind a grid when I change the value of a combobox. 我使用Telerik-MVC,并且在更改组合框的值时尝试重新绑定网格。 Everything seems working except I can't get selected value of my combobox. 一切似乎都正常,但是我无法获得组合框的选定值。 Here is my code : 这是我的代码:

Grid : 网格:

@{Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(o => o.Col1);
            columns.Bound(o => o.Col2);
            columns.Bound(o => o.Col3);
        })
        .DataBinding(dataBinding => dataBinding.Ajax()
             .Select("_MyAction", "MyController")
        )
        .ClientEvents(events => events.OnDataBinding("Grid_onDataBinding"))
}

Combobox : 组合框 :

@(Html.Telerik().ComboBox()
        .Name("ComboBox")
        .HtmlAttributes(new { id = "ComboBoxCode" })
        .BindTo(new SelectList(Model.GroupBy(x => x.Code)
                              .Select(o => o.First()).ToList(), 
                              "Code", "Code"))
        .ClientEvents(events => events
                .OnChange("onComboBoxChange")
        )
)

Script : 剧本:

function onComboBoxChange(e) {
    $("#Grid").data("tGrid").rebind();
}
function Grid_onDataBinding(e) {
    var code = /* I need to get the combobox value here */;
    e.data = { myCode: code };
}

Controller : 控制器:

    [GridAction]
    public ActionResult _MyAction(string myCode)
    {
        Console.WriteLine("Code : " + code);
        /*
            Set new model here
        */
        return View(new GridModel(newModel));
    }

I tried things like : 我尝试过类似的事情:

var e = document.getElementById("ComboBoxCode");
var code = e.options[e.selectedIndex].text;

But it doesn't work. 但这是行不通的。 Can you tell me how to fix this problem and get the right value ? 您能告诉我如何解决此问题并获得正确的价值吗?

The problem is that when you rebind the grid, you didn't really use the ComboBox selected value. 问题在于,当重新绑定网格时,您实际上并没有使用ComboBox选定的值。 Your onComboBoxChange function tells the grid to rebind, which it does by going to the _MyAction method. 您的onComboBoxChange函数告诉网格重新绑定,方法是_MyAction方法。 At no point did you pass in the ComboBox value. 您从未传入ComboBox值。 What you should do is before databinding, grab the selected value and pass it to your controller action. 您应该做的是在进行数据绑定之前,获取选定的值并将其传递给控制器​​操作。 See http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-client-api-and-events.html#OnDataBinding for details. 有关详细信息,请参见http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-client-api-and-events.html#OnDataBinding Something like this: 像这样:

function Grid_onDataBinding(e) {
    var combobox = $('#ComboBox').data('tComboBox');
    e.data = { code: combobox.value };
}

(Note that I actually haven't done any work with the ComboBox, so this isn't tested, but this should at least get you on the right path. See ComboBox documentation .) (请注意,我实际上并未对ComboBox进行任何工作,因此未经测试,但这至少应该使您走上正确的道路。请参阅ComboBox文档 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM