简体   繁体   English

Kendo UI Grid MVC重定向到强类型视图

[英]Kendo UI Grid MVC redirect to strongly typed view

I have two strongly typed views. 我有两个强类型的视图。 The first view is responsible for displaying a Kendo UI grid (MVC) which lists summarized grant applications. 第一个视图负责显示Kendo UI网格(MVC),其中列出了汇总的拨款申请。 When a user selects a row, they will be able to click a button that will redirect them to another View which is strongly typed as the Model represented by that grid row. 当用户选择一行时,他们将能够单击一个按钮,将其重定向到另一个视图,该视图的类型为该网格行所代表的模型。

The second view will display a form with all the details of the grant application and allow an administrator to approve or reject the application with comments. 第二个视图将显示一个表格,其中包含拨款申请的所有详细信息,并允许管理员批准或拒绝带有批注的申请。

The problem that I'm having is getting the "View Application" button to pass the model back to my controller and then load the other View with the Model selected. 我遇到的问题是获得“视图应用程序”按钮以​​将模型传递回控制器,然后使用选定的模型加载另一个视图。

Controller 调节器

    [HttpPost]
    public ActionResult ValidateModel(GrantFormDTO appToUpdate)
    {
        if (ModelState.IsValid)
        {
            return Json(appToUpdate);
        }

        return Json(new GrantFormDTO());
    }

    [HttpPost]
    public ViewResult Details(GrantFormDTO appToUpdate)
    {
        return View(appToUpdate);
    }

Kendo UI Grid Kendo UI网格

    @(Html.Kendo().Grid(Model)
      .Name("GrantApplications")
      .DataSource(dataSource => dataSource
          .Ajax()
          .Read(read => read.Action("Applications_Read", "GrantForm"))
      )
      .Columns(columns =>
      {
          columns.Bound(c => c.RequestingOrg).Width(150);
          columns.Bound(c => c.Telephone).Width(150);
          columns.Bound(c => c.Email).Width(150);
          columns.Bound(c => c.ContactName).Width(150);
          columns.Bound(c => c.ContactTel).Width(150);
          columns.Bound(c => c.AmountRequested).Width(150);
          columns.Bound(c => c.TotalGoal).Width(150);
          columns.Bound(c => c.Sponsor).Width(150);
          columns.Bound(c => c.Status).Width(150);
          columns.Bound(c => c.Comment).Width(150);
      })
      //.Scrollable(s => s.Enabled(true).Height("auto"))
      .Pageable()
      .Sortable()
      .Resizable(resize => resize.Columns(true))
      .Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
      .Events(events => events.Change("GetSelectedRow"))
      )

Scripts for getting selected row data and posting to controller 用于获取选定行数据并将其发布到控制器的脚本

<div class="row">
<div class="col-md-2 col-md-offset-10">
    <button class="btn btn-primary btn-block" id="viewApplicationBtn" type="button">View Application</button>
</div>

<script>
    //Selection changed event handler
    GetSelectedRow = function(event) {
        var grid = event.sender;
        var data = grid.dataItem(this.select());

        if (data == null) {
            $("#viewApplicationBtn").prop("disabled", true);
        } else {
            $("#viewApplicationBtn").prop("disabled", false);
        }
    }; 

    //View Application Button
    $("#viewApplicationBtn").prop("disabled", true);
    $("#viewApplicationBtn").on("click", function () {
        var grid = $("#GrantApplications").data("kendoGrid");
        var gridData = grid.dataItem(grid.select());

        $.ajax({
            type: "POST",
            url: "@Url.Action("ValidateModel")",
            datatype: "json",
            contentType: "application/json",
            data: JSON.stringify(gridData),
            success: function(data) {
                //This is where I think I need to do something with my Mode/redirect
            },
            error: function(data) {
                console.log("post failed");
            }
        });
    });
</script>

Since I'm doing an ajax call here, I'm not able to load a View from a controller method. 由于我在这里进行ajax调用,因此无法从控制器方法加载View。 The other answers I've seen related to this say to simply pass the parameters to my controller method in the .success function of my ajax call as url string variables and then let the model binder put it all together for me on the server side. 我看到的与此相关的其他答案是,只需将参数作为url字符串变量传递给我的ajax调用的.success函数中的控制器方法作为url字符串变量,然后让模型绑定器在服务器端将它们全部组合在一起。 That's fine, but I have a lot of fields to map, and I will be adding more, so I'd rather not go down that route. 很好,但是我要映射很多字段,并且我将添加更多字段,所以我宁愿不走那条路。

What I'd like to do, is have my JSON stringified row passed to my controller method, have it bind to my Model (all of which it does so far) and then load my strongly typed view of that Model. 我想做的是将JSON字符串化的行传递到我的控制器方法,将其绑定到我的模型(到目前为止所有这些操作),然后加载该模型的强类型视图。 How can I accomplish this? 我该怎么做?

If I can't use an ajax call, what other approach will let me pass my model from a kendo grid to my controller and let me redirect with it? 如果我不能使用ajax调用,还有什么其他方法可以让我将模型从kendo网格传递到控制器并进行重定向?

Ajax calls are mainly meant to be used when you want to update parts of the page from which you are making the call. Ajax调用主要用于要更新进行调用的页面部分时。 If you wanted to display the details on the same page as the grid, ajax would make sense. 如果您想在网格的同一页上显示详细信息,则使用ajax是有意义的。 Since you want the new view to be displayed in a new page, one suggestion would be to include the grid in a form and add the JavaScript to capture the grid data to the submit button click event (You can send the id of the grant to the server if you want to retrieve it again from the database). 由于您希望新视图显示在新页面中,因此建议将网格包含在表单中,并添加JavaScript以将网格数据捕获到Submit Button click事件中(您可以将Grant ID发送给服务器(如果您想再次从数据库中检索它)。 The form would be submitted to the Details method which will return the details view in a new page. 表单将被提交到Details方法,该方法将在新页面中返回details视图。

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

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