4

I'm learning and testing how to pass custom linq results

code from controller:

public ActionResult Index()
{
    const int pageSize = 5;
    return View(from p in db.powners
                  where p.petowner.StartsWith("")
                  orderby p.petowner.Skip(0).Take(pageSize).ToList()
                  select new { p.ownerid, p.petowner, p.ostreet });

}

code from view:

@model System.Collections.Generic.IEnumerable<Mvc4test2.Models.powner>


@{
    ViewBag.Title = "Index";
}
<link href="../../Content/PagedList.css" rel="stylesheet" type="text/css" />
<h2>Find owner</h2>

<p>
@using (@Html.BeginForm("index", "lookup", FormMethod.Get))
{    
    <b>Search</b>@Html.TextBox("search")<input type="submit" value="search" />
}
</p>
<table id="ownertable">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.petowner)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ostreet)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
    <td>
        <a href="">  @Html.DisplayFor(modelItem => item.ownerid) </a>
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.petowner)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ostreet)
    </td>

</tr>
}

</table>

What I have tried:

@model IEnumerable<Mvc4test2.Models.powner> 

and

@model System.Collections.Generic.IEnumerable<Mvc4test2.Models.powner>

Get following error:

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery1[<>f__AnonymousType43[System.Int32,System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Mvc4test2.Models.powner]'.`

Any idea how to pass this query to view and have it work as expected. Of course later I will use a variable at Skip(0). I have to learn to pass it first. Thanks

1
  • This expression works: Commented Dec 2, 2013 at 23:42

2 Answers 2

4

Don't pass anonymous type. Here I suggest you have two options. If Powner class has only 3 fields (ownerid, petowner, ostreet) then select new { p.ownerid, p.petowner, p.ostreet } line in your query is redundant.

public ActionResult Index()
{
     const int pageSize = 5;
     var model = (from p in db.powners
                 where p.petowner.StartsWith("")
                 orderby p.petowner.Skip(0).Take(pageSize) select p).ToList();
     return View(model);
}

or if your Powner class is more complicated and your view has to display only ownerid, petowner and ostreet than you should create view model class which contains of these 3 properties only. Example:

public class PownerViewModel
{
    public int OwnerId {get;set;}     // You should modify these
    public string Petowner {get;set;} // properties types
    public string OStreet {get;set;}  // since I don't exactly know what they are
}

.. and modify your query:

public ActionResult Index()
{
     const int pageSize = 5;
     var model = from p in db.powners
                 where p.petowner.StartsWith("")
                 orderby p.petowner.Skip(0).Take(pageSize)
                 select new PownerViewModel()
                 {
                     OwnerId = p.ownerid,  
                     Petowner = p.petowner,
                     OStreet = p.ostreet                          
                  };
     return View(model);
}

.. and of course change model type in your view:

@model System.Collections.Generic.IEnumerable<PownerViewModel>

P.S. There can be some errors or typos since I coded right here.

Sign up to request clarification or add additional context in comments.

5 Comments

This expression works: return View(db.powners.Where(x => x.petowner.StartsWith(search) || search == null).OrderBy(x => x.petowner).Skip(5).Take(pageSize).ToList()); What would be a regular linq to sql expression equivalent to this. Isn't this one called lamada or something like that?
@user560131, yes, this is alternative way of querying.
Works:return View(db.powners.Where(x => x.petowner.StartsWith(search) || search == null).OrderBy(x => x.petowner).Skip(5).Take(pageSize).ToList()); Doesn't work: return View(db.powners .Where(p => p.petowner.StartsWith(search)) .OrderBy(p => p.petowner).Skip(0).Take(5) .Select(p => new { p.ownerid, p.petowner, p.ostreet }).ToList()); WHY does the one with select NOT work? Guess Lambda doesn't require a Select clause. However, 2nd query works webforms app. Why doesn't it work in MVC?
Because, as I already said, your model is of anonymous type when you use .Select(p => new { p.ownerid, p.petowner, p.ostreet }) and your view is strongly typed. I don't understand why you need select, in this case it is redundant. However, if you really need select use .Select(p => new Powner { ownerid = p.ownerid, petowner = p.petowner, ostreet = p.ostreet }).
@DmytroTsiniavskyi hi...can u tell me or can u provide any link for how to get these query result in jquery method in view?
2

The problem is with the return type of your query in the Index controller. You should return enumerator of P.

Please check the below link:

passing the correct type from the Controller to the View?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.