0

我正在使用 SPA MVC 4 构建 Web 应用程序。我想知道是否有一些方法可以在 MVVM 中过滤和排序数据?我已经阅读了几个教程,但没有工作。

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net- mvc-应用程序

public ViewResult Index(string sortOrder, string searchString)
        {
            ViewBag.FirstNameSortParm = String.IsNullOrEmpty(sortOrder) ? "FirstName desc" : "";
            ViewBag.LastNameSortParm = sortOrder == "LastName" ? "LastName desc" : "LastName";

            using (DnaPatientTrackerEntities1 DptEntity = new DnaPatientTrackerEntities1())
            {
                var UserGroup = from s in DptEntity.Users
                                select s;

                if (!String.IsNullOrEmpty(searchString))
                {
                    UserGroup = UserGroup.Where(s => s.FirstName.ToUpper().Contains(searchString.ToUpper())
                                           || s.LastName.ToUpper().Contains(searchString.ToUpper()));
                }

                switch (sortOrder)
                {
                    case "FirstName desc":
                        UserGroup = UserGroup.OrderByDescending(s => s.FirstName);
                        break;
                    case "LastName":
                        UserGroup = UserGroup.OrderBy(s => s.LastName);
                        break;
                    case "LastName desc":
                        UserGroup = UserGroup.OrderByDescending(s => s.LastName);
                        break;
                    default:
                        UserGroup = UserGroup.OrderBy(s => s.FirstName);
                        break;
                }
                ViewBag.Users=UserGroup;
                return View();
            }

http://www.asp.net/single-page-application/spa-samples/sample-bigshelf-application/big-shelf-code

  self.selectSortOption = function (users) {
        self.nav.navigate({ sort: users.LastName, page: 1 });
    }

我在 KnockOut js 主页中发现我们可以获得一个自定义 javascript 函数,但是当我尝试创建用于排序/过滤的自定义函数时,我的 index.cshtml 出现加载错误。它自动导航到编辑器视图...

如果您有任何想法,请与我分享

更新:

我正在使用 SPA MVC4,所以我的视图包括:_Editor.cshtml、_Grid.cshtml、_Paging.cshtml、_Index.cshtml。我正在处理 _Grid.cshtml

_Grid.cshtml:

<h2>Users (<span data-bind="text: users().length"></span>)</h2>

@using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString")  
        <input type="submit" value="Search" /></p>
}
<tr>
    <th></th>
    <th>
        @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.FirstNameSortParm })
    </th>   
    <th>
        @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.LastNameSortParm })
    </th>
</tr>
<p><a href="#" data-bind="click: sortUserByFirstName">LastName</a></p>
<table>
    <thead>
        <tr>
             <th>UserID</th>
             <th>CreatedDate</th>
             <th>UpdatedDate</th>
             <th>FirstName</th>
             <th>MiddleName</th>
             <th>LastName</th>             
             <th>Status</th>
        </tr>
    </thead>

    <tbody data-bind="foreach: users">
    @*<tr><a href="#" data-bind="text: text, 
                   click: $parent.selectSortOption,
                   css: { selected: $parent.nav.params().sort == id }">Sort</a></tr>*@
        <tr data-bind="css: { added: IsAdded, updated: IsUpdated, deleted: IsDeleted, error: EntityError }">
            <td data-bind="text: UserID"></td>
            <td data-bind="text: CreatedDate"></td>
            <td data-bind="text: UpdatedDate"></td>
            <td data-bind="text: FirstName"></td>
            <td data-bind="text: MiddleName"></td>
            <td data-bind="text: LastName"></td>            
            <td data-bind="text: StatusText"></td>
            <td><a href="#" data-bind="click: $parent.openEditor">Edit</a></td>
        </tr>
    </tbody>
</table>

<p><a href="#" data-bind="click: createUser">Create new</a></p>
<button data-bind="click: saveAll">Save all</button>
<button data-bind="click: revertAll">Revert all</button>
4

0 回答 0