在ASP.NET MVC中将模型从View传递给控制器 [英] Passing Model to Controller from View in ASP.NET MVC

查看:99
本文介绍了在ASP.NET MVC中将模型从View传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ASP.NET MVC开发我的第一个应用程序,即使阅读了整个Internet,也遇到了我无法弄清的障碍.

I'm working on my first application in ASP.NET MVC and have run into a snag that I cannot figure out, even after reading the entire internet.

因此,我有几个使用视图模型创建的视图(它们是报告),这些视图是根据用户选择标准填充的.我正在尝试建立一个接受模型并以Excel格式导出它的方法(使用EPPlus),但是该模型需要从这些视图之一发送.这在某种程度上是功能/可用性,而不是技术要求.用户可以运行报告,并且在查看报告后,可以单击按钮/链接,然后将创建他们正在查看的报告的Excel版本.我想传递正在显示的视图所基于的模型,因为已经完成了根据用户的报告选择标准来构建此特定视图模型的工作.

So I have several Views (they are reports) that are created with View Models, which are populated based on user selection criteria. I'm trying to build a method that accepts a Model and exports it in Excel format (using EPPlus), but the Model needs to be sent from one of these Views. This is somewhat of a functional/usability, rather than a technical requirement. A user can run reports, and after viewing the report, can click a button/link and an Excel version of the report they're looking at will be created. I want to pass the Model that the View that's being displayed is based on since the work has already been done to build this specific View Model based on the user's report selection criteria.

这一切都很好,而且很好,我可以成功地将显示报告的视图中的模型传递给构建报告的方法.我通过的视图模型中的所有数据似乎都在那里,除了其中一部分模型(IEnumerable)之外.<-那就是我被困住的地方.IEnumerable的集合在断点处有0个项目,但是在创建视图模型并将其最初传递给视图时绝对不是这种情况.

This is all fine and good, and I can successfully pass a Model from a View that's displaying a report to the method that builds the report. All of the data in the View Model I pass seems to be there, except a collection of Models (IEnumerable) that is part of it. <--That is where I'm stuck. The collection of IEnumerable has 0 items in it at breakpoint, but this is definitely not the case in when the View Model is created and passed into the View initially.

我被困住了-请帮忙.

查看模型:

public class ReportViewModel
{

    public int ProjectId { get; set; }

    [Display(Name = "Project Name")]
    public string ProjectName { get; set; }

    [Display(Name = "Project #")]
    public string ProjectNumber { get; set; }

    public IEnumerable<SystemModel> SelectedSystems { get; set; }//has items when created and passed into the View intially, but not when the Model is passed from the View to the method/controller. 

    [Display(Name = "All Systems")]
    public bool AllSystems { get; set; }

    [Display(Name = "In Progress Systems")]
    public bool InProgressSystems { get; set; }

    [Display(Name = "Completed Systems")]
    public bool CompletedSystems { get; set; }

    [Display(Name = "Unchecked Systems")]
    public bool UncheckedSystems { get; set; }

    [Display(Name = "Systems with Problems - Ours")]
    public bool JciIssue { get; set; }

    [Display(Name = "Systems with Problems - Other's")]
    public bool OtherIssue { get; set; }

    [Display(Name = "Include Points with Problems")]
    public bool IncludePoints { get; set; }

}

查看:

    @model ProjectOps_5.ViewModels.ReportViewModel


@{
    ViewBag.Title = "Software Cx Status Report";
}

<h2>Software Cx Status Report</h2>

<div>
    <div>
        <hr />

 @Html.ActionLink("Export to Excel", "ExportExcel", Model)//call to method

//the report

方法:

    public void ExportExcel(ReportViewModel model)
        {
           //make Excel file
        }

推荐答案

您不能将包含复杂对象或集合属性的模型传递给GET.在内部, ActionLink()方法调用每个属性的 .ToString()方法,因此 DefaultModelBinder 基本上试图将属性设置为如下

You cannot pass a model that contains properties that are either complex objects or collections to a GET. Internally the ActionLink() method calls the .ToString() method of each property so basicaly the DefaultModelBinder is trying to set the property to as follows

model.SelectedSystems = "System.Collections.Generic.IEnumerable<SystemModel>";

哪个当然会失败,并且属性为 null .

which of course fails and the property is null.

幸运的是,它不起作用,否则您将生成一个非常丑陋的查询字符串,并且很容易超出查询字符串的限制并引发异常.

Fortunately it does not work, or you would generate a really ugly query string and could easily exceed the query string limit and throw an exception.

相反,传递 ProjectId 属性(假定唯一标识对象),然后在 ExportExcel()方法中再次获取模型

Instead, pass the ProjectId property (I'm assuming that uniquely identifies the object) and get the model again in the ExportExcel() method

@Html.ActionLink("Export to Excel", "ExportExcel", new { ID = Model.ProjectId })

并在控制器中

public void ExportExcel(int ID)
{
  // Get your ReportViewModel based on the ID and do stuff
}

这篇关于在ASP.NET MVC中将模型从View传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆