简体   繁体   English

Kendo DropDownList始终将相同的值从视图传递到控制器

[英]Kendo DropDownList always passing the same value from view to controller

Good morning/afternoon/evening and I apologize for the inconveniences. 早上/下午/晚上好,给您带来的不便深表歉意。

I have the following kendo sub-grid that has a simple dropdown with options: 我有以下kendo子网格,该子网格具有带选项的简单下拉列表:

<script id="subgrid" type="text/kendo-tmpl">
@(Html.Kendo().Grid<AccrualProfileDetailsModel>()
.Name("grid_#=acrpr_id#")
.Columns(columns =>
{
    columns.Bound(c => c.acrpr_id).Hidden();
    columns.Bound(c => c.acp_id).Hidden();
    columns.Bound(c => c.pc_name).Title(Resource.PayCode);
    columns.ForeignKey(c => c.acr_function, StaticLists.GetAccrualCodeFunction(), "Value", "Description")
           .Title(Resource.Type);
    columns.Command(command =>
    {
        command.Edit().Text(" ").HtmlAttributes(new { title = Resource.Edit + " " + Resource.AccrualProfile }).UpdateText(" ").CancelText(" ");
    }).Width("10%");
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.ToolBar(t =>
{
    t.Template(
        @<text>
            <div style="display:inline">
                @(Resource.Select + " " + Resource.AccrualCode + "  "):
                @(Html.Kendo().DropDownList()
                          .Name("accrualProfileDetails_#=acrpr_id#")
                          .DataValueField("acp_id")
                          .DataTextField("acp_name")
                          .BindTo(AccrualCodes)
                          .Events(ev => ev.Change("function(args) {accrualProfileDetailsDrpDwnChange(args, 'grid_#=acrpr_id#');}"))
                          .ToClientTemplate()
                )
            </div>
        </text>);
})
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(10)
    .Model(model =>
    {
        model.Id(p => p.acrpr_id);
        model.Field(p => p.acr_function).Editable(true);
        model.Field(p => p.pc_name).Editable(false);
    })
.Read(read => read.Action("ReadAccrualProfilesDetails", "AccrualProfiles").Data("GetSubGridParams(#=acrpr_id#)"))
.Update(update => update.Action("UpdateAccrualProfileDetail", "AccrualProfiles"))
)
.Events(ev => ev.Save("function(e) {getDropDownListValue(e, #=acrpr_id#)}"))
.ToClientTemplate()
)

Every time that the value of the DropDownList changes, it will trigger a read event that refreshes the records present in the subgrid. 每当DropDownList的值更改时,它将触发读取事件,该事件刷新刷新子网格中存在的记录。 The function that fires the read event is the following: 触发read事件的函数如下:

function accrualProfileDetailsDrpDwnChange(e, gridId) {
    var grid = $("#" + gridId).data("kendoGrid");
    grid.dataSource.read();
}

If you see this line: 如果看到此行:

.Read(read => read.Action("ReadAccrualProfilesDetails", "AccrualProfiles").Data("GetSubGridParams(#=acrpr_id#)"))

the READ event has attached a function that is called GetSubGridParams that, in theory, should do something like this: READ事件附加了一个名为GetSubGridParams的函数,从理论上讲,它应该执行以下操作:

function GetSubGridParams(acrpr_id) {
    var acp_id = $('#accrualProfileDetails_' + acrpr_id).data().kendoDropDownList.value();
    return {
        acrpr_id: acrpr_id,
        acp_id: acp_id
    };
}

It captures the id from the grid and the value of the DropDownList that will be passed to the controller as parameters. 它从网格捕获ID,并将DropDownList的值作为参数传递给控制器​​。

The problem is that every time the value of the DropDownList changes, the value passed to the controller IS ALWAYS THE SAME, it doesn't matter which option you choose from the DropDownList. 问题在于,每当DropDownList的值更改时,传递给控制器​​的值总是相同的,从DropDownList中选择哪个选项都没有关系。 I was searching on the web and on the Telerik documentation but the proposed solutions are always the same, but those solutions are not working for me. 我在网上和Telerik文档中进行搜索,但是建议的解决方案始终相同,但是这些解决方案对我而言不起作用。

My problem is simple: I need to pass to the controller the selected value of the DropDownList, something that the GetSubGridParams function is not doing. 我的问题很简单:我需要将DropDownList的选定值传递给控制器​​,这是GetSubGridParams函数无法执行的操作。

NOTE 注意

I saw that it is better to make a DropDownListFor instead of a simple DropDownList and binding it to an attribute from the model, but, as you can see, I can't do that because the DropDownList is inside a template and the model will be not recognized. 我看到最好制作一个DropDownListFor而不是简单的DropDownList并将其绑定到模型的属性,但是,正如您所看到的,我不能这样做,因为DropDownList在模板内部,并且模型将是未能识别。 Or maybe I don't know how to do it to recognize the model inside that template, eventually... Also, I tried to pass the values using ajax but the values still the same... 或者,也许我最终不知道如何识别该模板中的模型。此外,我尝试使用ajax传递值,但值仍然相同...

Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

After several tests, I got this thing working. 经过几次测试,我使这件事起作用了。 I made it work passing the data that was going from the View to the Controller via the "Change" event attached to the DropDownList, the accrualProfileDetailsDrpDwnChange function. 我使它通过附加到DropDownList的accrualProfileDetailsDrpDwnChange函数的“ Change”事件将要从View传递到控制器的accrualProfileDetailsDrpDwnChange工作。 After deleting the GetSubGridParams because it was not working as I was expecting, I edited that accrualProfileDetailsDrpDwnChange function as follows: 在删除GetSubGridParams因为它没有按预期运行,我对accrualProfileDetailsDrpDwnChange函数进行了如下编辑:

function accrualProfileDetailsDrpDwnChange(e, gridId, acrpr_id) {
    var grid = $("#" + gridId).data("kendoGrid");
    var acp_id = $('#accrualProfileDetails_' + acrpr_id).data().kendoDropDownList.value();
    var url = '@Url.Action("ReadAccrualProfilesDetails", "AccrualProfiles")' + "?acrpr_id=" + acrpr_id + "&acp_id=" + acp_id;
    grid.dataSource.transport.options.read.url = url;
    grid.dataSource.read();
}

The most important part is this: 最重要的部分是:

var url = '@Url.Action("ReadAccrualProfilesDetails", "AccrualProfiles")' + "?acrpr_id=" + acrpr_id + "&acp_id=" + acp_id;
    grid.dataSource.transport.options.read.url = url;
    grid.dataSource.read();

After setting the URL to the grid the parameters acrpr_id and acp_id are correctly sent from the View to the Controller as I wanted :D and every time the "READ" event from the grid is fired, the content is correctly shown. 在将URL设置为网格之后,参数acrpr_idacp_id会按照我想要的:D从View正确发送到控制器,并且每次从网格触发“ READ”事件时,内容都会正确显示。 That was my way to make it work. 那是我使其工作的方式。

Cheers. 干杯。

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

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