2

嗨,伙计们,在将 PagedList 与 ViewModel 一起使用时,确实需要您的帮助来解决问题(与域模型完美配合)。

我是 MVC 初学者,这是我迄今为止尝试过的。

/ *****我有一个模型*********** /

public class Property
{        
    public int ID { get; set; }
    public int UserID { get; set; }
    public DateTime Created { get; set; }
    public DateTime Edited { get; set; }
}

从中创建 ViewModel(将暂时仅使用一个域模型从其他域模型添加更多属性

public class SearchResultsVM
{

    public PagedList.IPagedList<Property> BasicDetails { get; set; }

}

控制器动作方法:

public ActionResult SearchResults(string sortOrder, string SearchKeyword, string currentFilter, int? page)
{
    ViewBag.CurrentSort = sortOrder;
    ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
    if (SearchKeyword != null){page = 1;}
    else{SearchKeyword = currentFilter;}
    ViewBag.CurrentFilter = SearchKeyword;            
    var sr = from s in db.Property
                          select s;          
    if (!String.IsNullOrEmpty(SearchKeyword))
    {sr = db.Property.Where(s =>   s.PropertyTitle.ToUpper().Contains(SearchKeyword.ToUpper()));}
    switch (sortOrder)
    {
        case "title_desc":
            sr = sr.OrderByDescending(s => s.PropertyTitle);
            break;
        case "Date":
            sr = sr.OrderBy(s => s.Created);
            break;
        case "date_desc":
            sr = sr.OrderByDescending(s => s.Created);
            break;
        default:
            sr = sr.OrderBy(s => s.PropertyTitle);
            break;
    }
    SearchResultsVM srVM = new SearchResultsVM();
    int pageSize = 10;
    int pageNumber = (page ?? 1);
    srVM.BasicDetails = sr.ToList().ToPagedList(pageNumber,pageSize);
    return View(srVM);
}

我的观点

在 View 中访问 ViewModel 时,我没有获得 Property Domain Model 的任何属性

@model PagedList.IPagedList<GH_Final.ViewModel.SearchResultsVM>
@using PagedList;
@using PagedList.Mvc;

@using (Html.BeginForm("SearchResults", "Home", FormMethod.Get))
{
<p>
    Find by name: @Html.TextBox("SearchKeyword", ViewBag.CurrentFilter as string)
    <input type="submit" value="Search" />
</p>
}


@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.created)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BasicDetails.Created)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BasicDetails.PropertyDesc)
        </td>
4

2 回答 2

2

代替:

@model PagedList.IPagedList<GH_Final.ViewModel.SearchResultsVM>

做这个:

@model GH_Final.ViewModel.SearchResultsVM

而不是:

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.created)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BasicDetails.Created)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BasicDetails.PropertyDesc)
        </td>

它应该是:

@foreach (var item in Model.BasicDetails)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Created)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Edited)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PropertyDesc)
        </td>
    <tr>
}
于 2014-04-14T15:19:37.070 回答
1

你也应该有你的表达方式:

@Html.DisplayFor(modelItem => item.First().Created)
@Html.DisplayFor(modelItem => item.First().Edited)
@Html.DisplayFor(modelItem => item.First().PropertyDesc)
于 2015-04-26T11:50:43.067 回答