是否创建所有模型属性的多选列表?

解析疯子

在我的MVC5 Code-First应用程序中,我正在使用EPPlus进行一些导出到Excel功能的工作我正在使用的ExportController - Index.cshtml视图是我的视图。我尝试遍历的模型是我的主要模型INV_Assets

我在几个类似的问题中看到了以下内容(仅用于测试的复选框)

@foreach (var property in Model.GetType().GetProperties())
{
    @Html.Label(property.Name)
    @Html.CheckBox(property.Name)
}

但是它没有渲染我的模型属性?比如我有这样的属性[owner][note][description],等,但是呈现在我看来,唯一的复选框标题为“容量”,“伯爵”和“项目”?

以下是我的控制器操作和视图:

ExportController-索引

@using GridMvc.Html
@using System.Collections.Generic
@model  List<InventoryTracker.Models.INV_Assets>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Export</h2>

@*@foreach (var prop in Model.GetType().GetProperties())
{
    @(Html.TextBox(prop.Name, prop.GetValue(Model, null)))
}*@

@*@foreach(var property in ViewData.ModelMetadata.Properties)
{
    <label>@(property.DisplayName??property.PropertyName)</label>
    @Html.Editor(property.PropertyName)
}*@

@foreach (var property in Model.GetType().GetProperties())
{
    @Html.Label(property.Name)
    @Html.CheckBox(property.Name)
}


<a href="/Export/Export" class="btn btn-default btn-sm noDecoration"><span class="glyphicon glyphicon-export"> Export</span></a>
<br />
<br />
<a href="/Export/ExportUsingEPPlus" class="btn btn-default btn-sm noDecoration"><span class="glyphicon glyphicon-export"> Export - EPPlus</span></a>

ExportController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using InventoryTracker.DAL;
using OfficeOpenXml;


namespace InventoryTracker.Controllers
{
    public class ExportController : Controller
    {
        InventoryTrackerContext _db = new InventoryTrackerContext();

        // GET: Export
        public ActionResult Index()
        {
            var assetList = _db.INV_Assets.ToList();
            return View(assetList);
        }

        public ActionResult Export()
        {
            GridView gv = new GridView();
            gv.DataSource = _db.INV_Assets.ToList();
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=InventoryAssets-" + DateTime.Now + ".xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

            return RedirectToAction("StudentDetails");
        }

        public ActionResult ExportUsingEPPlus()
        {

            //FileInfo newExcelFile = new FileInfo(output);
            ExcelPackage package = new ExcelPackage();
            var ws = package.Workbook.Worksheets.Add("TestExport");
            ws.Cells["A1"].Value = "Sample Export 1";


            var memoryStream = new MemoryStream();
            package.SaveAs(memoryStream);

            string fileName = "Exported-InventoryAssets-" + DateTime.Now + ".xlsx";
            string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            memoryStream.Position = 0;
            return File(memoryStream, contentType, fileName);

        }

    }

    //https://stackoverflow.com/questions/5796151/export-model-data-to-excel-mvc
    //https://landokal.wordpress.com/2011/04/28/asp-net-mvc-export-to-excel-trick/


}

我想要做的是获取所有INV_Assets属性的多选列表,然后仅将这些属性导出EPPlus到Excel。


编辑

型号代码INV_Assets

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using GridMvc.DataAnnotations;
using System.Web.Mvc;
using InventoryTracker.Models;

namespace InventoryTracker.Models
{
    [GridTable(PagingEnabled = true, PageSize = 30)]
    public class INV_Assets 
    {
        // Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model.


        public int Id { get; set; }

        public int Model_Id { get; set; }
        [ForeignKey("Model_Id")]
        public virtual INV_Models Model { get; set; }

        [Required]
        public int Manufacturer_Id { get; set; }
        [ForeignKey("Manufacturer_Id")]
        public virtual INV_Manufacturers Manufacturer { get; set; }

        [Required]
        public int Type_Id { get; set; }
        [ForeignKey("Type_Id")]
        public virtual INV_Types Type { get; set; }

        [Required]
        public int Location_Id { get; set; }
        [ForeignKey("Location_Id")]
        public virtual INV_Locations Location { get; set; }

        public int Vendor_Id { get; set; }
        [ForeignKey("Vendor_Id")]
        public virtual INV_Vendors Vendor { get; set; }

        [Required]
        public int Status_Id { get; set; }
        [ForeignKey("Status_Id")]
        public virtual INV_Statuses Status { get; set; }

        public string ip_address { get; set; }

        public string mac_address { get; set; }

        [DataType(DataType.MultilineText)]
        public string note { get; set; }
        public string owner { get; set; }

        //[DataType(DataType.Currency)]
        //[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode=true)]
        [DisplayFormat(DataFormatString = "{0:#,###0.00}", ApplyFormatInEditMode=true)]
        public decimal cost { get; set; }
        public string po_number { get; set; }

        [DataType(DataType.MultilineText)]
        public string description { get; set; }

        public int invoice_number{ get; set; }

        [Required]
        public string serial_number { get; set; }

        [Required]
        public string asset_tag_number { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? acquired_date { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? disposed_date { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? verified_date { get; set; }

        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime created_date { get; set; }

        [Required]
        public string created_by { get; set; }

        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
        public DateTime? modified_date { get; set; }

        public string modified_by { get; set; }

        // Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.)
        //public bool available { get; set; }
    }
}
丹尼尔·加布里埃尔

像这样尝试:

@Html.ListBox("PropertyList", typeof(INV_Assets).GetProperties().Select(p => new SelectListItem { Text = p.Name, Value = p.Name, Selected = false })

编辑:

更改了GetProperties()被调用的对象的类型在您调用它之前,Model这是一个列表。但是,您真正想做的是对存储在列表中的对象类型进行调用。

编辑2:

已更改为,ListBox而不是ListBoxFor由于您未使用视图模型,因此需要从RequestPOST上提取选定的项目您也List<string> propertyList可以在后操作中添加参数,参数应自动​​绑定。

编辑3:

要在发布操作中获取数据,您必须将列表框包装在一个表单中,如下所示:

@using (Html.BeginForm("ExportProps", "Export") {
    @Html.ListBox(...)
}

这是在POST操作中获取提交的数据的方法:

[HttpPost]
public ActionResult ExportProps(List<string> propertyList) {
    // do stuff with your data here
}

上面的代码未经测试,但是应该给出如何处理帖子的想法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

是否创建所有目录?

从任意深度列表的嵌套列表创建所有组合

Typescript-创建所有可用CSS属性的类型

创建所有可能性的列表

创建所有索引的列表-给定计数/大小

列表视图预先创建所有详细视图

创建所有用户的电子邮件列表

Python根据值列表创建所有字典的组合

如何从 python 中的元组列表创建所有排列?

是否可以使用列表推导从字典中创建所有成员字典键的列表?

是否可以创建所有主分区?

这件作品如何为给定列表创建所有子列表的列表?

有没有办法在spark中创建所有列的列表

创建所有索引列表的函数,其中元素出现在列表中

从二维列表创建所有可能的一维列表

Python:在已排序的嵌套列表中创建所有元素的新列表

如何从字符串列表中创建所有第n个字符的列表?

递归创建所有字母的目录

创建所有递增值序列

是否有必要在一开始在情节提要中创建所有视图?

class.newInstance 创建所有属性为空的对象

在Google Apps脚本中创建所有工作表名称的列表

从列表中的第一项创建所有序列

通过向量列表创建所有可能组合的算法功能

如何创建所有键均为唯一键的json对象列表

Impala / SQL:分组依据-通过创建所有值的列表来聚合字段

从Python中的素因子列表创建所有可能的因子分解

如何在使用R保留结构的同时从嵌套列表创建所有组合?

在Java中,如何从特定的正则表达式创建所有可能数字的列表?