简体   繁体   English

MVC 4 ASP.NET中的订单列表,即控制器接收2个变量

[英]Order List in MVC 4 ASP.NET i.e. Controller receiving 2 variables

I am trying to rewrite the default MVC Index controller that displays the list of records from a database. 我试图重写默认的MVC索引控制器,该控制器显示数据库中的记录列表。 I am trying to have an order by function on 1 of the headings AND the ability to search on the page: 我正在尝试按标题中的1个功能排序并在页面上进行搜索:

Controller code: 控制器代码:

public ActionResult Index(String sortOrder, String searchString) {
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
        var profiles = from s in db.Profiles
                       select s;

        if (!String.IsNullOrEmpty(searchString))
        {
            profiles = profiles.Where(d => d.lastName.ToUpper().Contains(searchString.ToUpper()));
        }
        switch (sortOrder)
        {
            case "Name_desc":
                profiles = profiles.OrderByDescending(s => s.lastName);
                break;
            default:
                profiles = profiles.OrderBy(s => s.lastName);
                break;
        }
        return View(profiles.ToList());
    }

View Code: 查看代码:

<th>
    @Html.ActionLink("Last Name", "Index", new { sortOrder = "Name_desc" })
</th>

I have been trying various ways, but it seems as if I am not succeeding with the @html.actionLink calling the controller method 我一直在尝试各种方法,但是好像我没有成功使用@ html.actionLink调用控制器方法

Followed this tutorial exact but not working: 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-application 完全按照本教程操作,但不起作用: http : //www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-在asp-net-mvc应用程序中

So to explain how I solved the answer... 因此,为了解释我如何解决答案...

The actionResult was a HttpPost back. actionResult是一个HttpPost返回。 So it would only accept input when a button is clicked. 因此,仅在单击按钮时它将接受输入。 It seems as if @HTML.actionlinks only call the view methods. 似乎@ HTML.actionlinks仅调用视图方法。

I simply created a new Index(String sortOrder, String searchString) method and the view code was @Html.ActionLink("Last Name", "/Index", new { sortOrder = ViewBag.NameSortParm}) 我只是创建了一个新的Index(String sortOrder, String searchString)方法,视图代码为@Html.ActionLink("Last Name", "/Index", new { sortOrder = ViewBag.NameSortParm})

Its messy, but solved my issue nonetheless 它很杂乱,但仍然解决了我的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC 4表单发布变量在接收控制器中为null - ASP.NET MVC 4 form post variables are null in receiving controller 使用Razor在Asp.net MVC中显示重新登录(即注销后再次登录)上的最后一个cookie值 - Showing last cookie value on Relogin (i.e. Again login after logout) in Asp.net MVC with Razor 我在 ASP.NET MVC 应用程序的 controller 上收到服务器错误 - I am receiving a Server error on on the controller on my ASP.NET MVC app 将一般的 ASP.NET 控件视为基于列表的控件(即 DropDownList、RadioButtonList 等) - Treat a general ASP.NET Control like a list-based control (i.e. DropDownList, RadioButtonList, etc.) ASP.NET MVC View没有接收传递给它的列表 - ASP.NET MVC View not receiving list passed into it ASP.NET MVC Controller中的执行顺序是什么? - What is the order of execution in ASP.NET MVC Controller? 将空列表从控制器传递到控制器ASP.NET MVC - Passing empty list from controller to controller asp.net mvc 通知用户有关帐户锁定的信息,即在ASP .NET MVC2下 - Informing user about account-lock i.e. under ASP .NET MVC2 ASP.NET - MVC 从控制器发送和接收模型到视图并返回控制器 - ASP.NET - MVC Sending and receiving model from controller to view and back to controller ASP.NET Core 托管在 Windows 服务中 - 未提供静态文件(即 /wwwroot 的内容) - ASP.NET Core hosted in a Windows Service - static files not being served (i.e. contents of /wwwroot)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM