How to pass custom linq query to view
asp.net-mvc-4
Solution
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.
Problem
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.ObjectQuery`1[<>f__AnonymousType4`3[System.Int32,System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[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