Rgodinho
Rgodinho

Reputation: 1

ASP.NET Get data for Details error

i have this error

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

my controller

public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var results = from c in db.Suppliers
                  where (c.ID == id)
                  select c;

    /*Suppliers suppliers = db.Suppliers.Find(id);*/
    if (results == null)
    {
        return HttpNotFound();
    }

    return View(results);
}

View

 @model  Teste.Models.Suppliers

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

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

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

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

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

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

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

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

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

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

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

How i can solve this?

Solved

I made a new project and worked well ;) thx all

Upvotes: 0

Views: 64

Answers (1)

Darren Wainwright
Darren Wainwright

Reputation: 30747

You need to return the actual Supplier. You are returning your linq query

So change

 if (results == null)
 {
     return HttpNotFound();
 }
 return View(results);

To

 Supplier supplier = results.FirstOrDefault();
 if (supplier == null)
 {
     return HttpNotFound();
 }
 return View(supplier);

Calling FirstOrDefault() will run your query and return either the first match or null.

Upvotes: 2

Related Questions