在View中显示来自不同表的数据

时间:2018-07-31 07:00:00

标签: c# asp.net .net razor model-view-controller

只想问一下我是否想念这里。 我有两个在控制器中连接在一起的表 使用.NET MVC btw 无论如何,这是我的控制器:

namespace Review.Controllers
{
    public class ReviewController : Controller
    {
        ReviewContext db = new ReviewContext();
        ReviewItemsContext db2 = new ReviewItemsContext();
        MainDataModel db3 = new MainDataModel();

        List<UAR_Review> uar_review = new List<UAR_Review>();
        List<UAR_ReviewItems> uar_reviewitems = new List<UAR_ReviewItems>();


        public ActionResult Index(int? page) {

            ViewBag.AccList = (from r in db2.UAR_ReviewItems
                               select r.Account).Distinct();

            /*var entities = from s in db2.UAR_ReviewItems
                            orderby s.Account
                            select s;*/

            var entities = from s in uar_review
                           join st in uar_reviewitems on s.ID equals st.ReviewID into st2
                           from st in st2.DefaultIfEmpty()
                           select new MainDataModel { UAR_Review = s, UAR_ReviewItems = st };


            int pageSize = 15;
            int pageNumber = (page ?? 1);
            return View(entities.ToPagedList(pageNumber, pageSize));
        }
    }
}

这是我的观点:

@model PagedList.IPagedList<Review.Models.MainDataModel>
@using PagedList.Mvc;

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Index";
}


@using (Html.BeginForm("Index", "Review", FormMethod.Get))
{
    <h2>Index</h2>

    @section NavBar{

        <h3>Period: 2 of 2018</h3>

        @Html.DropDownList("userAccount", new SelectList(ViewBag.AccList), "Select Account to Filter")
        <input type="submit" value="Search" />    
        }

    @section MiddleSection{

        <table class="table">
            <tr>
                <th>
                    <span class="arrow-link contrast-large light-blue">@Html.DisplayName("Name")</span>
                </th>
                <th>
                    <span class="arrow-link contrast-large light-blue">@Html.DisplayName("Role")</span>
                </th>
                <th>
                    <span class="arrow-link contrast-large light-blue">@Html.DisplayName("Action")</span>

                </th>
            </tr>

            @foreach (var item in Model)
            {

                <tr>
                    <td>
                       <span class="font-gotham-narrow">@item.UAR_Review.DisplayName</span>
                    </td>
                    <td>
                       <span class="font-gotham-narrow">@item.UAR_ReviewItems.Role</span>
                    </td>
                    <td>
                        @Html.HiddenFor(modelItem => item.UAR_ReviewItems.Response)
                        <span class="font-gotham-narrow">
                            @Html.RadioButton("Response", "Retain")@Html.Label("Retain")&emsp;
                        </span>
                        <span class="font-gotham-narrow">
                            @Html.RadioButton("Response", "Remove")@Html.Label("Remove")
                        </span>
                    </td>
                </tr>
            }
        </table>
        <br />
        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

        @Html.PagedListPager(Model, page => Url.Action("Index",
            new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

        }
    }
}

问题是,当我运行它时,网站上没有任何显示。here's the page.

我只是想问一下我是否想念任何东西。 另外,请忽略左侧的下拉列表,我一直打算使用它通过AccountType(存储在我的UAR_ReviewItems表中)对页面中显示的元素进行排序。现在,我只是想弄清楚这里出了什么问题以及为什么没有显示任何元素。

1 个答案:

答案 0 :(得分:0)

您的屏幕抓图确实显示PageCount为0。 您的模型为空。检查您的查询。