【问题标题】:ASP.NET return different enum types from the view to the same controller actionASP.NET 将不同的枚举类型从视图返回到相同的控制器操作
【发布时间】:2017-08-15 20:08:00
【问题描述】:

我有一个具有多个视图的控制器。每个视图都返回一个名为 actionType 的参数,其中每个视图都返回不同的枚举类型(例如 ElectricEnum、PaperlessEnum、DigitalEnum)。在我的控制器操作中,我将其设置为接收名为 actionType 的枚举类型。默认设置为空。但是,我无法让参数填充数据,无论如何它总是返回 null。

这是来自视图和方法头的代码示例...

查看:

@using (Html.BeginForm("DownloadCSVReport", "Public", FormMethod.Post, new { target = "_blank" }))
        {
            @Html.Hidden("fromDate", Model.StatsByDate.Parameters.FromDate)
            @Html.Hidden("toDate", Model.StatsByDate.Parameters.ToDate)
            @Html.Hidden("serviceType", ServiceTypeEnum.Paperless)
            @Html.Hidden("actionType", Model.StatsByDate.Parameters.ActionType)

            <input type="submit" class="btn btn-primary" name="download" value="Download" formmethod="post" />
        }

控制器:

public ActionResult DownloadCSVReport(DateTime fromDate, DateTime toDate, ServiceTypeEnum serviceType, Enum actionType = null)
    {
        string returnValue = _dashboardClient.Call(svc => svc.GetCommaSeparatedList(fromDate, toDate, serviceType, actionType));

        Response.ContentType = "application/ms-excel";
        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.csv", "ReportDownload"));
        Response.AppendCookie(new HttpCookie("DownloadToken", System.DateTime.Now.ToShortTimeString()));    // For loader icon

        return Content(returnValue, "application/vnd.ms-excel");
    }

在此视图中,ActionType 的值为 ElectronicEnum,但在调用此 DownloadCSVReport 操作的其他视图中,它们是不同类型的枚举...

是否可以通过这种方式解决这个问题,我可以将这些不同类型的枚举传递到一个目的地并利用它的值吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc enums


    【解决方案1】:

    模型绑定只是寻找模型属性。因此,不属于您的模型的任何内容都将丢失。将ActionType 添加到您的模型中,您将在控制器中将其作为模型的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 2019-12-29
      • 1970-01-01
      • 2021-04-05
      • 2019-09-28
      相关资源
      最近更新 更多