【问题标题】:ASP.NET MVC - Xml Export - formatting the file name according to class attributesASP.NET MVC - Xml Export - 根据类属性格式化文件名
【发布时间】:2019-07-31 15:20:23
【问题描述】:

我有这个类,它在网格中获取选定的文件

[HttpPost]
    public ActionResult ExportXml(string apontamentos)
    {
        try
        {
            string[] arrApontamentos = apontamentos.Split(';');
            var listApontamentoId = arrApontamentos.Select(x => new Guid(x)).ToList();
            var apontamentosViewModel = this._apontamentoAppService.ObterTodos(listApontamentoId);

            List<ApontamentoExportarViewModel> listXml = new List<ApontamentoExportarViewModel>();

            int item = 1;
            foreach (var informacaoApontamentoVM in apontamentosViewModel)
            {

                listXml.Add(new ApontamentoExportarViewModel
                {
                    Item = item,
                    Equipamento = informacaoApontamentoVM.Barco.SapId,
                    Atendimento = informacaoApontamentoVM.Atendimento,
                    Escala = informacaoApontamentoVM.LocalDaOperacao.Abreviacao,
                    DescricaoDaOperacao = informacaoApontamentoVM.CodigosDeOperacao.Descricao,
                    //GrupoDeCodigo = "xxx",
                    CodigoOperacao = informacaoApontamentoVM.CodigosDeOperacao.Codigo,
                    DataInicial = string.Format("{0:dd.MM.yyyy}", informacaoApontamentoVM.DataInicio),
                    HoraInicial = string.Format("{0:HH.mm.ss}", informacaoApontamentoVM.DataInicio),
                    DataFinal = string.Format("{0:dd:MM:yyyy}", informacaoApontamentoVM.DataTermino),
                    HoraFinal = string.Format("{0:HH:mm:ss}", informacaoApontamentoVM.DataTermino),
                    Observacoes = informacaoApontamentoVM.Observacao
                });
                item++;
            }
            var status = this._apontamentoAppService.ObterDescricaoStatusApontamento(Domain.Apontamentos.StatusApontamento.Exportado);
            this._apontamentoAppService.AtualizarStatus(apontamentosViewModel.Select(x => x.Id).ToList(), status);
            return new XmlActionResult<ApontamentoExportarViewModel>(listXml);
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

我还有另一个,它可以导出和 xml 格式和文件名

  public class XmlActionResult<T> : ActionResult
  {
    public XmlActionResult(List<T> data)
    {
        Data = data;
    }

    public List<T> Data { get; private set; }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "text/xml";

        // TODO: Use your preferred xml serializer 
        // to serialize the model to the response stream :
        // context.HttpContext.Response.OutputStream
        ApontamentoExportarViewModel apontamentoExportarViewModel = new ApontamentoExportarViewModel();
        var cd = new System.Net.Mime.ContentDisposition
        {
            // for example foo.bak
            FileName = string.Format("MA_"+ "Equipamento" + "_{0:dd-MM-yyyy_HH-mm-ss}.xml", DateTime.Now),


            // always prompt the user for downloading, set to true if you want 
            // the browser to try to show the file inline
            Inline = false,
        };
        var root = new XmlRootAttribute("meadinkent");
        XmlSerializer x = new XmlSerializer(Data.GetType(), root);
        context.HttpContext.Response.AppendHeader("Content-Disposition", cd.ToString());
        x.Serialize(context.HttpContext.Response.OutputStream, Data);


    }
}

}

基本上,我需要获取“Equipamento”属性并将其插入到文件名中。

“ApontamentoExportarViewModel”类的信息来自“Data”属性,但是如何在该列表中找到信息?记住我只需要属性“设备”的信息

如何将此属性的值带入 XmlActionResult 类?

【问题讨论】:

    标签: c# xml asp.net-mvc export


    【解决方案1】:

    嗯,您已经在 XmlActionResult 中的 Data 中找到了它,但是因为它是 List&lt;T&gt;,所以它可以是任何东西。在这种情况下,它是您的 ApontamentoExportarViewModel 视图模型。

    这个 XmlActionResult 方法应该能够处理各种类型的对象,还是只能处理ApontamentoExportarViewModel?如果有多种类型,那么并非每种类型都有Equipamento 属性。在这种情况下,您必须执行以下操作:

    var fileName = "default";
    if(Data is List<ApontamentoExportarViewModel>)
    {
        var record = (Data as List<ApontamentoExportarViewModel>).FirstOrDefault(); // what should happen if there's more than one?
        if (record != null)
            fileName = record.Equipamento;
    }
    

    然后:

    FileName = string.Format("MA_"+ fileName + "_{0:dd-MM-yyyy_HH-mm-ss}.xml", DateTime.Now);
    

    或者类似的东西。

    如果您确定进入您的方法的每个对象都将具有Equipamento 属性,那么您可以创建一个基类,从中派生具有Equipamento 或接口的视图模型类,并且然后将您的方法更改为不接受任何类型 (&lt;T&gt;),而只接受从基类派生/实现接口的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多