c# - Mvc 在 View 中使用 2 @model

标签 c# asp.net-mvc asp.net-mvc-5

大家好,我有一个将数据填充到表中的 View 我有这个想法

@model IEnumerable<"Model">

在我看来我有这样的事情

@foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Audit_ID)
            </td>
            //rest of the code
         </tr>
    }

我的问题是如何在 View 中输入一些东西

@model DAL.EODAuditModel
@model IEnumerable<"Model">

因为我想做这个

@using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
<fieldset>
            <legend>Employee</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
               @Html.EditorFor(model => model.Name)
               @Html.ValidationMessageFor(model => model.Name)
           </div>
</fieldset>
}

问题是我不能这样做,因为我在 View 中定义了 2 个@model,它会抛出异常

我想要这个因为这个 View 将查看记录并创建新记录。 谢谢。

更新 2:我根据建议编辑我的代码库

模型代码:

public class EODAuditModel
    {
        public EODAuditModel()
        {
            this.ExistingRecords = new List<EODAuditModel>();
        }
        public int Audit_ID { get; set; }
        [Required]
        public string Description { get; set; }
        [Required]
        public string CreatedBy { get; set; }
        public DateTime CreatedDate { get; set; }
        public List<EODAuditModel> ExistingRecords { get; set; }
    }

查看代码:

@foreach (var item in Model.ExistingRecords)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreatedBy)
            </td>

        </tr>
    }

Controller 代码:

public ActionResult Index()
        {
            EODAuditBL eodBl = new EODAuditBL();
            List<EODAuditModel> eodModel = eodBl.GetEODAudit.ToList();
            return View(eodModel);
        }

获取EODAuditCode:

public IEnumerable<EODAuditModel> GetEODAudit
{
}

当我运行程序时,编译正常,但我在 网页

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[DAL.EODAuditModel]', but this dictionary requires a model item of type 'DAL.EODAuditModel'.

最佳答案

您可以创建一个包含 IEnumerable<"Model"> 的新模型类和 Name属性(property)。

public class AuditViewModel
{
    public AuditViewModel()
    {
        this.ExistingRecords = new List<EODAuditModel>();
    }
    public string Name { get; set; }
    public List<EODAuditModel> ExistingRecords { get; set; }
}

在你的 View 中定义模型如下

@model AuditViewModel

并更改 foreach阻止这个

@foreach (var item in Model.ExistingRecords)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Audit_ID)
        </td>
        //rest of the code
     </tr>
}

更新

看起来您遇到了错误,因为在 Controller 中您传递了一个 List<DAL.EODAuditModel> 的实例而不是 DAL.EODAuditModel到 View 。将您的 Controller 更改为此

public ActionResult Index()
{
    EODAuditBL eodBl = new EODAuditBL();
    List<EODAuditModel> auditList = eodBl.GetEODAudit.ToList();

    EODAuditModel model = new EODAuditModel();
    model.ExistingRecords = auditList;

    return View(model);
}

我改变了eodModelauditList以避免混淆模型和现有记录。

关于c# - Mvc 在 View 中使用 2 @model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26123498/

相关文章:

asp.net-mvc - ASP.NET MVC 4 操作过滤器问题

c# - 尝试访问 Amazon s3 上的文件时出现 SignatureDoesNotMatch 错误

c# - 如何为 ASP.NET MVC 5 应用程序设置时区?

c# - Redis - 通过一些 "key"获取单个元素

.net - ViewState 在 ASP.NET MVC 中是否相关?

c# - C# 中的 Jack Sensing

asp.net-mvc - 使用 url 值作为路由值中的变量

entity-framework - Unity单例多请求访问ASP.Net MVC 5

c# - 如何将 jQueryUI(变量)与 MySQL 查询结合使用

c# - 在 C# 中查找字符串中的子字符串