【问题标题】:The model item passed into the dictionary is of type " something" The model item passed into the dictionary is of type "something else"传入字典的模型项的类型为“something” 传入字典的模型项的类型为“something else”
【发布时间】:2019-06-01 00:29:53
【问题描述】:

我意识到这里有太多类似的问题。但我无法用这些解决方案解决我的问题。

 datingDbEntities db = new datingDbEntities();

  [Authorize]
  [HttpGet]
  public ActionResult FindMatch()
  {
      return View();
  }


 [HTTPPost]
 public ActionResult FindMatch(string searchString)
    {  
        var users = from m in db.userAcc
                    select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            users = users.Where(s => s.userAd.Contains(searchString));
        }

        return View(users);
    }

这是我的行为

@model IEnumerable<datingo.Models.EntityFramework.userAcc>


<h2>Index</h2>


<p>
    @Html.ActionLink("FindMatch", "Home")

    @using (Html.BeginForm("FindMatch", "Home", FormMethod.Post))
    {
     <p>
        Title: @Html.TextBox("SearchString") <br />
        <input type="submit" value="Filter" />
     </p>
     }

这是我的看法。

namespace datingo.Models.EntityFramework
{
    using System;
    using System.Collections.Generic;

 public partial class userAcc
 {
    public int userId { get; set; }
    public string userName { get; set; }
    public string userPw { get; set; }
    public string userMail { get; set; }
    public Nullable<bool> userGender { get; set; }
    public string userAd { get; set; }
    public string userSoyad { get; set; }
    public Nullable<int> userBoy { get; set; }
    public Nullable<int> userKilo { get; set; }
    public string userHair { get; set; }
    public string userEye { get; set; }
    public string userCountry { get; set; }
    public string userFavTeam { get; set; }
    public string userBurc { get; set; }
    public string userFavMusic { get; set; }
    public string userFavFilm { get; set; }
    public string userMeslek { get; set; }
    public string userEgitim { get; set; }
    public byte[] userPhoto { get; set; }
    public Nullable<System.DateTime> userBirthday { get; set; }
    public int commentParentId { get; set; }
  }
}

如果你需要,这是我的模型。

所以,我试图获取搜索结果,但是当我点击提交按钮时,它给了我

“传入字典的模型项的类型是'System.Data.Entity.Infrastructure.DbQuery`1[datingo.Models.EntityFramework.userAcc]',但是这个字典需要一个'datingo.Models类型的模型项.EntityFramework.userAcc'。”

错误。顺便说一下,我知道我没有为表格列表添加所需的视图,但至少现在对我来说没有必要。我不知道该怎么做,有很多关于这个列表和搜索操作的教程,他们工作正常,但我的不是。

【问题讨论】:

  • 尝试返回 View(users.ToList());
  • @NaDeRStar 还是一样,只是错误中的“DbQuery”改为“List”
  • 标题为“字典”。代码中没有字典,所以你的意思是 DATABASE?
  • @jdweng 我没有任何意思,我对错误一无所知,我使用的是 sql 数据库,所以如果你问的话,我需要从那里提取结果
  • 如果您要在 (var user = ...) linq 中添加 where 子句怎么办?类似于 where m.userAd == searchString 并在 select m 之后添加 .ToList()

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


【解决方案1】:

尝试将return View(users);改为return View(users.ToList());

【讨论】:

  • 不,还是一样,只是错误中的“DbQuery”更改为“List”
【解决方案2】:

我解决了我的问题。

在我看来我用过

@model IEnumerable<datingo.Models.EntityFramework.userAcc>

如你所记。

而且在我提到的布局中,我使用了另一个@model,比如

@model datingo.Models.EntityFramework.userAcc

当我删除布局模型部分时,它是固定的。

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 2011-02-14
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多