-1

Hey guys i'm stuck on an issue and I searched the site before qnd found solutions but I can't seem to find the problem in my code. So I was hoping somebody more experienced could help me out here.

I want to put something into an input box on the main index.cshtml and then send it with to search for this input.

Home/index.cshtml from search onward is whats not working.

@model webApp.Models.Table

@{
    ViewBag.Title = "Home Page";
 }

 <div class="jumbotron">
    <h1>WebApp</h1>
    </div>

    <div class="row">
       <div class="col-md-4">
       <h2>Index</h2>
     <p>
        @Html.ActionLink("Database lisst", "Index", "Tables")<br/>
    </p>
</div>
<div class="col-md-4">
    <h2>Search</h2>
    <p>
        @using (Html.BeginForm("Search", "Tables", FormMethod.Post))
        {
        <p>
            Email: @Html.TextBoxFor(model => model.email) <br />

            <input type="submit" value="Filter" />
        </p>
        } 
    </p>
</div>

Search.cshtml

@model webApp.Models.Table

@{
    ViewBag.Title = "Search";
 }

 <h2>Search</h2>

<div>
<h4>Table</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.email)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.email)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.code)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.code)
    </dd>

</dl>
</div>
<p>
   @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
   @Html.ActionLink("Back to List", "Index")
</p>

controller

public ActionResult Search(string emailString)
    {
        var search = from m in db.Table select m;
        if (!String.IsNullOrEmpty(emailString))
        {
            search = search.Where(s => s.email.Contains(emailString));
        }

        return View(search);
    }

I'm very inexperienced in mvc and asp.net so any help would be really nice.

EDIT: Forgot the mention the actual issue, I keep getting this error at the moment.

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

Tourna
  • 15
  • 1
  • 6
  • What exactly is wrong? Have you tried debugging it? From a cursory look, it looks like `webApp.Models.Table.email`'s name isn't matching with `emailString`. – James Haug Feb 21 '17 at 14:49
  • The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[webApp.Models.Table]', but this dictionary requires a model item of type 'webApp.Models.Table'. I keep getting this error – Tourna Feb 21 '17 at 14:54
  • 1
    Your query generates a collection of `Table` which you pass to a view that expects a single `Table` - no idea what you want but its either `return View(search.FirstOrDefaut());` to display one, or change the view to `@model IEnumerable` and use a loop to display the properties for each object.
    –  Feb 21 '17 at 22:38

3 Answers3

0

It seems that the name of the input field that you're posting is email, while the parameter in your Search action is emailString.

Try changing emailString to email and I think it will work. Also, consider GET instead of POST, unless you're trying to hide it from query string for some reason or not allow direct access.

Tony Basallo
  • 3,000
  • 2
  • 29
  • 47
0

Real problem is from your view Table class model is passing and you are capturing it in string In Controller action

Change your Action Code to this:-

public ActionResult Search(Table model)
    {
        var search = from m in db.Table select m;
        if (!String.IsNullOrEmpty(model.email))
        {
            search = search.Where(s => s.email.Contains(model.email));
        }

        return View(search);
    }
Ghanshyam Singh
  • 1,361
  • 2
  • 15
  • 27
0

You are passing a query to the view. With FirstOrDefault you execute the query and get a single result (no result returns null). Furthermore your form is passing a model to the controller. Add .ToLower() to avoid case sensitivity.

public ActionResult Search([Bind("email")] Table table)
{
    var search = db.Table.Where(s => s.email.ToLower()
                                     .Contains(table.email.ToLower())
                                     .FirstorDefault());
    //do something if search = null
    return View(search);
}
m3dix
  • 46
  • 1
  • 7