简体   繁体   English

将复杂对象从视图发送到 MVC 操作作为无 ajax 调用

[英]Send complex object from view to MVC action as none ajax call

I'm working on an ASP.NET MVC web application.我正在开发一个ASP.NET MVC Web 应用程序。 There is a view with a bunch of search filters and there is also a grid in the middle of the page that represents the result of the search.有一个带有一堆搜索过滤器的视图,页面中间还有一个表示搜索结果的网格。

End-user set value for the search filter and when search button clicked an ajax call returns the values that will rebind the gird.最终用户为搜索过滤器设置值,当单击搜索按钮时,ajax 调用将返回将重新绑定网格的值。 every row in the grid represents ' ItemId ' and ' version ' which could be selected by the checkbox.网格中的每一行都代表“ ItemId ”和“ version ”,它们可以通过复选框选择。

At the end when the user clicks on " Detail report " we need to redirect the user to another page(view).最后,当用户点击“ Detail report ”时,我们需要将用户重定向到另一个页面(视图)。

I need to pass the selected search filters and the grid selected rows values as an array like this (ItemIds[] and Versions[]) to the " Detail " action.我需要将选定的搜索过滤器和网格选定的行值作为这样的数组(ItemIds[] and Versions[])传递给“ Detail ”操作。

So, to make the question clear.所以,把问题说清楚。 I need to pass the below values to the action :我需要将以下值传递给操作:

  1. search filters搜索过滤器
  2. ItemId array ItemId 数组
  3. Version array版本数组

Unfortunately, I can not pass the parameters to the action.不幸的是,我无法将参数传递给操作。 I got null我得到了空

I can not call the action as ajax call When i use当我使用时,我不能将操作称为 ajax 调用

View ( Index.cshtml )查看 ( Index.cshtml )

 function prepareSelectedInfos() {
            var formValues = $('#form').serializeFormToObject();
            var gridValues = GetGridSelectedValues();
            var dto = {
                FormFilters: formValues,
                ItemIds : gridValues.ItemIds,
                Versions : gridValues.Versions
            } 
            return dto;
        }


  $('#lnkDetailReport').click(function () {
            var gridValues = GetGridSelectedValues();

            var url = '/Report/Controller/Detail/';
            var dto = prepareSelectedInfos();
            window.open(url + dto, '_blank');
        });

Controller控制器

  public ActionResult Detail(ModelVersionStatisticalDetailDto data)
        {

            //create a ViewModel according to the incoming data
            var viewModel = ;
            return View(viewModel);
        }

Model模型

  public class ModelVersionStatisticalDetailDto
    {
        public ModelVersionStatisticalReportDto FormFilters { get; set; }
        public int [] ItemIds { get; set; }
        public string[] Versions { get; set; }
    }

I need to have the view prepared values in the Detail action我需要在 Detail 操作中准备好视图值

To the best of my recollection, it is not possible to perform a redirect after having post a complex object in ajax call (with the same posted parameters).据我所知,在 ajax 调用中发布复杂对象(使用相同的发布参数)后,无法执行重定向。 In order to perform your desired operation, you can choose a solution from below.为了执行您想要的操作,您可以从下面选择一个解决方案。

  1. Use query strings in your Detail action and perform a simple post action instead of ajax callDetail操作中使用查询字符串并执行简单的 post 操作而不是 ajax 调用
  2. Try to replace the response of Detail action with Partial View尝试用Partial View替换Detail action 的响应
  3. If you are seeking a client side solution, you can assist from localStorage object (a tricky solution).如果您正在寻求客户端解决方案,您可以从 localStorage 对象(一个棘手的解决方案)中提供帮助。 You can store the required data in a localStorage object and pass the key to the controller.您可以将所需数据存储在 localStorage 对象中,并将密钥传递给控制器​​。 In the target page, use the key in order to fetch the data stored in storage.在目标页面中,使用键来获取存储在存储中的数据。 Do not forget to clear it after the process.完成后不要忘记清除它。 https://blog.logrocket.com/the-complete-guide-to-using-localstorage-in-javascript-apps-ba44edb53a36/ https://blog.logrocket.com/the-complete-guide-to-using-localstorage-in-javascript-apps-ba44edb53a36/

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

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