0

i realised there is too many similar question in here.But I can't solved my problem with any of those solutions.

 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);
    }

This is my actions

@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>
     }

This is my view.

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; }
  }
}

and this is my model if you need.

So, im trying to get search results but when i click submit button its giving me the

"The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[datingo.Models.EntityFramework.userAcc]', but this dictionary requires a model item of type 'datingo.Models.EntityFramework.userAcc'."

error.By the way, I know i didnt add needed view for table listing but thats not necessary for me at least now.I don't know what to do, there is many tutorial about this listing and searching actions, they are working properly but mine is not.

Kadir Akın
  • 43
  • 1
  • 7
  • try return View(users.ToList()); – NaDeR Star Jan 05 '19 at 20:11
  • @NaDeRStar still same, just the "DbQuery" changed to "List" in the error – Kadir Akın Jan 05 '19 at 20:15
  • The title says "Dictionary". There is no dictionary in the code so do you mean DATABASE? – jdweng Jan 05 '19 at 20:16
  • @jdweng i didn't mean anything, i dont have any idea about error, im using sql database, so i need to pull results from there if you asking about it – Kadir Akın Jan 05 '19 at 20:18
  • What if you were to add a where clause to your (var user = …) linq? Something like where m.userAd == searchString and after the select m add .ToList() – nocturns2 Jan 05 '19 at 20:28
  • Try return View(users.AsEnumerable()); so... – NaDeR Star Jan 05 '19 at 20:42
  • @nocturns2 sorry, didn't work.But anyway if code works your suggestion should give me one to one results, i need similar results – Kadir Akın Jan 05 '19 at 20:45
  • @NaDeRStar still same :( – Kadir Akın Jan 05 '19 at 20:47
  • Try this var users = new List(); if (!String.IsNullOrEmpty(searchString)) { users = db.userAcc.Where(s => s.userAd.Contains(searchString)).ToList()); } return View(users); – NaDeR Star Jan 05 '19 at 20:51
  • @NaDeRStar var users = List(); it says "non-invocable member 'List' cannot be used like method. i deleted () part, i tried List and it says " 'List' is a type, which is not valid in the given context." – Kadir Akın Jan 05 '19 at 20:56
  • Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) – Tetsuya Yamamoto Jan 07 '19 at 03:19

2 Answers2

0

Try to change return View(users); to return View(users.ToList());

derodevil
  • 811
  • 1
  • 11
  • 37
0

i solved my problem.

In my view i used

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

as you remember.

And also in my referred layout i was using another @model like

@model datingo.Models.EntityFramework.userAcc

when i deleted the layout model part it is fixed.

Kadir Akın
  • 43
  • 1
  • 7