如何将自定义linq查询传递给视图

时间:2013-12-02 01:05:53

标签: asp.net-mvc-4

我正在学习并测试如何传递自定义linq结果

来自控制器的代码:

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

}
来自视图的

代码:

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

我尝试过:

@model IEnumerable<Mvc4test2.Models.powner> 

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

获取以下错误:

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery 1并[d&GT; 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]”`

任何想法如何传递此查询以查看并使其按预期工作。 当然稍后我将在Skip(0)中使用变量。我必须先学会传递它。 感谢

2 个答案:

答案 0 :(得分:4)

不要传递匿名类型。在这里,我建议你有两个选择。如果Powner类只有3个字段(ownerid,petowner,ostreet),则查询中的select new { p.ownerid, p.petowner, p.ostreet }行是多余的。

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

或者如果您的Powner类更复杂,并且您的视图必须仅显示ownerid,petowner和ostreet,则应创建仅包含这3个属性的视图模型类。例如:

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
}

..并修改您的查询:

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

..当然会在您的视图中更改模型类型:

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

P.S。因为我在这里编码,可能会有一些错误或拼写错误。

答案 1 :(得分:2)

问题在于索引控制器中查询的返回类型。你应该返回P。

的枚举器

请查看以下链接:

passing the correct type from the Controller to the View?