我有带有文本输入、表格和寻呼机的页面。文本输入用于设置每页的表格行数。
查看代码:
@using (Html.BeginForm("Index", "Read", FormMethod.Get))
{
<p>
Rows per page: @Html.TextBox("cnt", ViewBag.cnt as int?)
<input type="submit" value="Refresh" />
</p>
}
<table>
<tr>
<th>
@Html.ActionLink("Code", "Index", new { Order = ViewBag.CodeSortParm, cnt = ViewBag.cnt })
</th>
<th>
@Html.ActionLink("Description", "Index", new { Order = ViewBag.TextSortParm, cnt = ViewBag.cnt })
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@item.Code
</td>
<td>
@item.Text
</td>
</tr>
}
</table>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager( Model, page => Url.Action("Index", new { page, cnt = ViewBag.cnt, Order = ViewBag.CurrentSort }) )
控制器代码:
public ActionResult Index(string Order, int? page, int? cnt)
{
ViewBag.CurrentSort = Order;
ViewBag.CodeSortParm = String.IsNullOrEmpty(Order) ? "Code_desc" : "";
ViewBag.TextSortParm = Order == "Text" ? "Text_desc" : "Text";
ViewBag.cnt = cnt;
Tbl T = new Tbl();
IQueryable<ErrCode> ErrCodes = T.GetErrCodes();
switch (Order)
{
case "Code_desc":
ErrCodes = ErrCodes.OrderByDescending(x => x.Code);
break;
case "Text":
ErrCodes = ErrCodes.OrderBy(x => x.Text);
break;
case "Text_desc":
ErrCodes = ErrCodes.OrderByDescending(x => x.Text);
break;
default:
ErrCodes = ErrCodes.OrderBy(x => x.Code);
break;
}
int pageSize = cnt ?? 3;
int pageNumber = (page ?? 1);
var onePageOfProducts = ErrCodes.ToPagedList(pageNumber, pageSize);
return View(onePageOfProducts);
}
当我按下“刷新”按钮时,它只传递给控制器 Cnt 值。但我也想传递 Order 值。我尝试了这个变体:
@Html.TextBox("cnt", ViewBag.cnt as int?, new { Order = ViewBag.CurrentSort })
和这个:
Html.BeginForm("Index", "Read", FormMethod.Get, new { Order = ViewBag.CurrentSort })
但它不起作用(不将订单参数传递给控制器)。提交表单时将订单参数传递给控制器的正确方法是什么?