c# - ViewModel 不包含电子邮件的定义

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

我正在尝试将两个 UserManager Feedback 模型与一个 View 绑定(bind),但我遇到了问题,因为 View 不包含电子邮件的任何定义...此处是我的所有代码

public  class FeedbackMixModel
    {

        public List<UserManager> allUserManagers { get; set; }

        public List<Feedback> allfeedbacks { get; set; }

    }

Controller ==>>>

  var user = db.UserManagers.ToList();
            var feedbacks = db.Feedbacks.ToList();

            var Model = new FeedbackMixModel
            {
                allUserManagers =user,
                allfeedbacks=feedbacks
            };

            return View(Model);

查看==>>

@model WebApplication5.Models.FeedbackMixModel


<!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->

        <!-- Main content -->


            <table class="pretty" id="dt">
                <thead>
                    <tr>
                        <th >
                            @Html.DisplayNameFor(model => model.Email)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.CurrentStorage)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.MaxStorage)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.LastLoginMonth)
                        </th>

                    </tr>
                </thead>
                <tbody>
                     @foreach (var item in Model.allUserManagers)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Email)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.CurrentStorage)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.MaxStorage)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.LastLoginMonth)
                            </td>

                        </tr>
                    }
                </tbody>
            </table>

我完全困惑如何解决这个问题...其中电子邮件在UserManager类中定义,如果我做错了什么,我如何访问

最佳答案

您的实际 FeedbackMixModel 模型本身没有您打算用于 header 的属性的定义:

<thead>
  <tr>
    <th>
      @Html.DisplayNameFor(model => model.Email)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.CurrentStorage)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.MaxStorage)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.LastLoginMonth)
    </th>
  </tr>
</thead>

您现有的模型仅包含两个属性:allUserManagersallFeedbacks,因此它不知道什么Email或其他一些属性你引用的是。根据您当前的代码,这些似乎是您的 UserManager 对象的实际属性,而不是您实际的 FeedbackMixModel 类。

您可以考虑对 header 进行硬编码以表达这些属性:

<thead>
    <tr>
        <th>
          Email
        </th>
        <th>
          Current Storage
        </th>
        <th>
          Max Storage
        </th>
        <th>
          Last Login Month
        </th>
      </tr>
 </thead>

或者您可以尝试定位集合中的第一个元素,类似于 this approach :

<thead>
    <tr>
        <th>
          @Html.DisplayNameFor(x => Model.allUserManagers[0].Email)
        </th>
        <th>
          @Html.DisplayNameFor(x => Model.allUserManagers[0].CurrentStorage)
        </th>
        <th>
          @Html.DisplayNameFor(x => Model.allUserManagers[0].MaxStorage)
        </th>
        <th>
          @Html.DisplayNameFor(x => Model.allUserManagers[0].LastLoginMonth)
        </th>
      </tr>
 </thead>

关于c# - ViewModel 不包含电子邮件的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106398/

相关文章:

asp.net-mvc-4 - Durandal.js : change navigation options per area

javascript - 将值从 View 传递到动态填充表单的 Controller

c# - ASP.Net MVC - 如何在 html 标签内显示来自 View 模型的字符串?

c# - MVVM:在 WPF NET 3.5 中完成线程池工作项后更新 View 的控件可见性

c# - 如何在模型和 View 模型中使用 "DRY up"C# 属性?

asp.net-mvc - MVC 应用程序中的后退按钮功能

asp.net-mvc-4 - MVC 站点随机不返回 PartialView

c# - 为什么要在 WinForm 对话框上使用 InitializeLifetimeService?

c# - 检测受密码保护的文档

asp.net-mvc - ASP.NET MVC 与 Azure B2C 刷新 ID token 问题