我有带文本输入,表格和寻呼机的页面。文本输入用于设置每页的表行数。
查看代码:
@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 })
但它不起作用(不将Order param传递给Controller)。
提交表单时将Order param传递给Controller的正确方法是什么?
答案 0 :(得分:2)
唯一可以发布的是Form中的内容。
因此,您可以放置@Html.Hidden("Order", ViewBag.CurrentSort)
。这将发布订单值,但不会显示。