在控制器方法之间传递数据

passing data between controller methods

我正在创建 asp.net mvc 5 应用程序。在此应用程序中,我遇到了在控制器方法之间传递数据的问题。

这里是场景一步一步

  1. 我正在将 IEnumerable 数据集获取到 Create_Brochure 方法,就像这样

    public ActionResult Create_Brochure(IEnumerable<ProductsPropertiesVM> model)
    {
    
        IEnumerable<BrochureTemplateProperties> sample = model.Where....
    
        return View(sample);
    }
    

  2. 然后我需要将 IEnumerable<ProductsPropertiesVM> model 保存到另一个 IEnumerable 对象并在 Create_Brochure_PDF() 方法

    中使用它
    public ActionResult Create_Brochure_PDF()
    {
    
        IEnumerable<BrochureTemplateProperties> samplePDF = modelPDF....
    
        return View(samplePDF);
    }
    

为此,我做了一些研发工作,并提出了 Sessions 的解决方案,这里 the tutorial 我跟进了

所以我像这样更改了我的代码

但似乎我遇到了编译时错误,尽管我完全按照教程进行操作

第一种控制器方法

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Create_Brochure(IEnumerable<ProductsPropertiesVM> model)
    {
        IEnumerable<ProductsPropertiesVM> modelPDF = new IEnumerable<ProductsPropertiesVM>();

        modelPDF = model;

       IEnumerable<BrochureTemplateProperties> sample = model.Where(y => y.IsChecked)
               .Select(y => new BrochureTemplateProperties
               {
                   Property_ID = y.Property_ID,
                   IsChecked = y.IsChecked,
                   Property_Title = y.Property_Title,
                   Property_Value = y.Property_Value
               });

        TempData["TemplateData"] = modelPDF;

        return View(sample);
    }

第二种控制器方法

    public ActionResult Create_Brochure_PDF()
    {

        IEnumerable<ProductsPropertiesVM> modelPDF = TempData["TemplateData"] as IEnumerable<ProductsPropertiesVM>;

        IEnumerable<BrochureTemplateProperties> samplePDF = modelPDF.Where(y => y.IsChecked)
                .Select(y => new BrochureTemplateProperties
                {
                    Property_ID = y.Property_ID,
                    IsChecked = y.IsChecked,
                    Property_Title = y.Property_Title,
                    Property_Value = y.Property_Value
                });

        return View(samplePDF);
    }

您不能实例化接口..!

替换

IEnumerable<ProductsPropertiesVM> modelPDF = new IEnumerable<ProductsPropertiesVM>();
modelPDF = model;

IEnumerable<ProductsPropertiesVM> modelPDF = model;

在您的 Create_Brochure 方法中。