【问题标题】:Viewmodel not working correctly MVC errorViewmodel 无法正常工作 MVC 错误
【发布时间】:2021-10-15 09:40:00
【问题描述】:

任何帮助都会很棒。我最近在 MVC 中启动了一个项目来存储一些客户端详细信息,我创建了模型、控制器,然后右键单击添加视图。我比较新,我试图在列表视图中显示查询结果,但收到以下错误:

传入字典的模型项的类型为“System.Collections.Generic.List1[<>f__AnonymousType148[System.String,System.String,System.Nullable1[System.DateTime],System.String,System.String,System.String,System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[cc_proj.ViewModels.HomeDashboardViewModel]”。

控制器:

    [HttpGet]
    public ActionResult Dash()
    {
     using (var context = new CRM_businessdevelopmentEntities())
    {
    var data =
    (from Clients in context.Clients
    join Sizes in context.Sizes on new { SizeID = (int)Clients.SizeID } equals new { SizeID = Sizes.SizeID }
    join CampaignTypes in context.CampaignTypes on new { CampaignTypeID = (int)Clients.CampaignTypeID } equals new { CampaignTypeID = CampaignTypes.CampaignTypeID }
    join Countrys in context.Countrys on new { CountryID = (int)Clients.CountryID } equals new { CountryID = Countrys.CountryID }
    join Contacts in context.Contacts on new { ContactID = (int)Clients.ContactID } equals new { ContactID = Contacts.ContactID }
    join Stages in context.Stages on new { StageID = (int)Clients.StageID } equals new { StageID = Stages.StageID }
    where
      Clients.Active == "True"
    orderby
      Clients.DateAdded descending
    select new
    {
        Stage = Stages.Description,
        Name = Clients.Name,
        DateAdded = Clients.DateAdded,
        Contact = Contacts.Contact,
        Location = Countrys.Country,
        CampaignType = CampaignTypes.Description,
        Size = Sizes.Description,
        Notes = Clients.Notes
    }).ToList();
    
    return View(data);

   }
}

视图模型:

public class HomeDashboardViewModel
{
    [Key]
    public int ClientID { get; set; }
    public string Stage { get; set; }
    public string Name { get; set; }
    public DateTime DateAdded { get; set; }
    public string Contact { get; set; }
    public string Location { get; set; }
    public string CampaignType { get; set; }
    public string Size { get; set; }
    public string Notes { get; set; }
    public string Active { get; set; }
}

查看:

    @model IEnumerable<Business_Development_CRM.ViewModels.HomeDashboardViewModel>

@{
    ViewBag.Title = "Dash";
}

<h2>Dash</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Stage)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DateAdded)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Contact)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Location)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CampaignType)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Size)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Notes)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Active)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Stage)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateAdded)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Contact)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Location)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CampaignType)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Size)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Notes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Active)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ClientID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ClientID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ClientID })
        </td>
    </tr>
}

</table>

【问题讨论】:

    标签: c# asp.net-mvc linq


    【解决方案1】:

    尝试修复查询,而不是匿名对象列表,而是创建 ViewModel 对象列表

     var data =
        (from Clients in context.Clients
         .....
        select new HomeDashboardViewModel
        {
            Stage = Stages.Description,
           .....
        }).ToList();
        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-14
      • 2017-02-10
      • 2015-02-15
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多