无法通过JavaScript将参数从视图传递到控制器

无法通过JavaScript将参数从视图传递到控制器,javascript,jquery,ajax,asp.net-mvc,parameter-passing,Javascript,Jquery,Ajax,Asp.net Mvc,Parameter Passing,虽然我设法将网格的选定行id发送给控制器,但无法建立打开操作视图的url。它是创建/controller/action而不是/controller/action/id。因此,当我尝试使用/controller/action/id打开视图时,它是打开的,但无法通过如下按钮单击打开 视图: Route.config: 是否有上述的另一个例子来传递参数,或者上面的代码是否有错误?提前谢谢 .ToolBar(toolbar => { toolbar.Template(@&l

虽然我设法将网格的选定行id发送给控制器,但无法建立打开操作视图的url。它是创建/controller/action而不是/controller/action/id。因此,当我尝试使用/controller/action/id打开视图时,它是打开的,但无法通过如下按钮单击打开

视图:

Route.config:

是否有上述的另一个例子来传递参数,或者上面的代码是否有错误?提前谢谢

.ToolBar(toolbar =>
    {
        toolbar.Template(@<text>
            <div class="toolbar">
            @(Html.Kendo().Button()
                .Name("addbtn")
                .Content("Add New")
                .HtmlAttributes(new { type = "button", @class = "k-primary k-button k-button-icontext js-myKendoButton", @data_id = @Model.YourID, onclick = "onClick()" })
            )
            </div>
        </text>);
    })
详情如下:

使用这个,我希望这将对您有所帮助

$('#btn').on("click",function () {
    var items = {};
    var grid = $('#Grid').data('kendoGrid');
    var selectedElements = grid.select();
    var item = grid.dataItem(selectedElements[0]);

        $.ajax({
            type: "GET",
            data: item.ID, //I obtained the id properly
            url: '@Url.Action("CreateParticipant", "Training")/'+item.ID,
            datatype:'html',
            success: function (result) {
              alert(result)
            }
        })
    })
或使用

$('#btn').on("click",function () {
    var items = {};
    var grid = $('#Grid').data('kendoGrid');
    var selectedElements = grid.select();
    var item = grid.dataItem(selectedElements[0]);

    window.location.href= '@Url.Action("CreateParticipant", "Training")/'+item.ID; 
});

希望这有帮助。

谢谢您的回复。@data\u id=@Model.id部分出错,因为它是一个列表而不是一条记录。在另一个hgand上,我尝试在HtmlAttributes中使用id值,但得到了相同的错误。因此,我认为在这种情况下,最好使用javascript方法并从HtmlatAttributes调用它。还有其他解决方案吗?有很多,获得ID参数更漂亮,ID参数可以传递给控制器。但是,无法调用相关视图。另一方面,当我在工作时键入呼叫地址时。但当通过按钮调用时,它的行为就像我调用一样,没有操作,也没有id参数。所以,在viewmodel方面没有问题。有什么想法吗?welcom,如果id为null,那么对您的模型没有影响,但首先要准备的是,如果id为null,则您的模型。我在控制器方法中检查了id是否为null,关于id值没有问题。好的,那么,你的下一个场景是什么呢?检查id是否为空,数据是否为find,然后以其他方式显示特定视图呈现相同的视图。我在alert中获得了相关视图的html。但是,在按下“确定”按钮或注释掉此警报行后,仍无法渲染相关视图。我尝试在另一个浏览器上清除它的所有历史记录和cookies,但仍然没有改变。真是难以置信,我已经有很多视图,并通过不同的选项打开这些视图,如超链接、图像、按钮等。您想在哪里显示此html?您必须将html附加到任何内容标记(如div)中,才能呈现上述控制器中调用的htmlIn CreateParticipant视图。只需尝试退出窗口,location.href='@Url.ActionCreateParticipant,Training/'+item.ID内部按钮单击删除ajax调用。我将更新我的答案。请参阅$.ajax用于异步加载页面,但window.location.href用于正常加载,这就是区别。
.ToolBar(toolbar =>
    {
        toolbar.Template(@<text>
            <div class="toolbar">
            @(Html.Kendo().Button()
                .Name("addbtn")
                .Content("Add New")
                .HtmlAttributes(new { type = "button", @class = "k-primary k-button k-button-icontext js-myKendoButton", @data_id = @Model.YourID, onclick = "onClick()" })
            )
            </div>
        </text>);
    })
$('.js-myKendoButton').attr('data-id');
<script>
$('#btn').on("click",function () {
    var items = {};
    var grid = $('#Grid').data('kendoGrid');
    var selectedElements = grid.select();
    var item = grid.dataItem(selectedElements[0]);

    $.ajax({
        type: "POST",
        data: {ID  : item.ID}, //I obtained the id properly
        url: '@Url.Action("CreateParticipant", "Training")',
        success: function (result) {
            //console.log(result);
        }
    })
})
</script> 
$('#btn').on("click",function () {
    var items = {};
    var grid = $('#Grid').data('kendoGrid');
    var selectedElements = grid.select();
    var item = grid.dataItem(selectedElements[0]);

        $.ajax({
            type: "GET",
            data: item.ID, //I obtained the id properly
            url: '@Url.Action("CreateParticipant", "Training")/'+item.ID,
            datatype:'html',
            success: function (result) {
              alert(result)
            }
        })
    })
$('#btn').on("click",function () {
    var items = {};
    var grid = $('#Grid').data('kendoGrid');
    var selectedElements = grid.select();
    var item = grid.dataItem(selectedElements[0]);

    window.location.href= '@Url.Action("CreateParticipant", "Training")/'+item.ID; 
});