我如何从这个 MVC 架构中获取参数?
How could I fetch a parameter from this MVC architecture?
我在 ASP .NET 项目中有此 HTML 页面,我需要为 table 元素生成分页。问题是,在已经存在的 class 架构中,我的模型唯一可以拥有的是视图模型的 IEnumerable。我唯一需要做的就是从我的控制器或视图中获取一个整数值 returns。从那个代表生成分页所需的按钮数量的整数,我会创建它,请参阅 HTML。
我的控制器在模型中生成项目列表和 returns 通过执行 SQL 请求从特定偏移量中选择一定数量的项目,具体取决于我的 URL的参数。
这是 HTML,控制器中的代码隐藏看起来像:
@model IEnumerable<ItemIndexViewModel>
<h2>@UiText.PageTitles.ITEM_LIST</h2>
<hr />
<div class="col-md-12">
<div class="col-md-9">
<table class="table" id="client-index">
<thead>
<tr>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Name)
</th>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Id)
</th>
</tr>
</thead>
<tbody>
@foreach (ItemViewModel item in Model)
{
@*Here, I have my table of items being generated*@
}
</tbody>
</table>
<div id="pagination">
<ul>
@for (int i = 0; i < [I need my int right here]; i++)
{
@*I will generate buttons here*@
}
</ul>
</div>
</div>
</div>
public virtual ActionResult Index()
{
int ownerId = _httpContext.GetUserOwnerId();
int amountPerPage = 0;
int pageIndex = 0;
Int32.TryParse(Request.QueryString["amountPerPage"], out amountPerPage);
Int32.TryParse(Request.QueryString["pageIndex"], out pageIndex);
if (amountPerPage <= 0)
{
amountPerPage = 10;
}
if (pageIndex <= 0)
{
pageIndex = 1;
}
List<Item> items = _itemRepository.GetByPage(pageIndex, amountPerPage).ToList();
// Make view models from the list of items
List<ItemIndexViewModel> itemIndexViewModels = Mapper.Map<List<ItemIndexViewModel>>(items);
// Create the buttons for the HTML
int totalAmount = _itemRepository.Count();
int totalPages = (Int32)Math.Ceiling(Decimal.Divide(totalAmount, amountPerPage));
// Set update the navigation trace
SetTraceRoot(MVC.Item.Index(), MVC.Item.ActionNames.Index);
return View(itemIndexViewModels.OrderBy(x => x.Name));
}
生成分页的好方法是什么?我正在寻求灵活性,因为此过程将针对不止一页和不止一个 class 项实施。我已经尝试了一些无济于事的方法,比如使用 class 来包含我的视图模型列表和一个整数来表示存储它们所需的页数。
我在 ASP .NET 项目中有此 HTML 页面,我需要为 table 元素生成分页。问题是,在已经存在的 class 架构中,我的模型唯一可以拥有的是视图模型的 IEnumerable。我唯一需要做的就是从我的控制器或视图中获取一个整数值 returns。从那个代表生成分页所需的按钮数量的整数,我会创建它,请参阅 HTML。
我的控制器在模型中生成项目列表和 returns 通过执行 SQL 请求从特定偏移量中选择一定数量的项目,具体取决于我的 URL的参数。
这是 HTML,控制器中的代码隐藏看起来像:
@model IEnumerable<ItemIndexViewModel>
<h2>@UiText.PageTitles.ITEM_LIST</h2>
<hr />
<div class="col-md-12">
<div class="col-md-9">
<table class="table" id="client-index">
<thead>
<tr>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Name)
</th>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Id)
</th>
</tr>
</thead>
<tbody>
@foreach (ItemViewModel item in Model)
{
@*Here, I have my table of items being generated*@
}
</tbody>
</table>
<div id="pagination">
<ul>
@for (int i = 0; i < [I need my int right here]; i++)
{
@*I will generate buttons here*@
}
</ul>
</div>
</div>
</div>
public virtual ActionResult Index()
{
int ownerId = _httpContext.GetUserOwnerId();
int amountPerPage = 0;
int pageIndex = 0;
Int32.TryParse(Request.QueryString["amountPerPage"], out amountPerPage);
Int32.TryParse(Request.QueryString["pageIndex"], out pageIndex);
if (amountPerPage <= 0)
{
amountPerPage = 10;
}
if (pageIndex <= 0)
{
pageIndex = 1;
}
List<Item> items = _itemRepository.GetByPage(pageIndex, amountPerPage).ToList();
// Make view models from the list of items
List<ItemIndexViewModel> itemIndexViewModels = Mapper.Map<List<ItemIndexViewModel>>(items);
// Create the buttons for the HTML
int totalAmount = _itemRepository.Count();
int totalPages = (Int32)Math.Ceiling(Decimal.Divide(totalAmount, amountPerPage));
// Set update the navigation trace
SetTraceRoot(MVC.Item.Index(), MVC.Item.ActionNames.Index);
return View(itemIndexViewModels.OrderBy(x => x.Name));
}
生成分页的好方法是什么?我正在寻求灵活性,因为此过程将针对不止一页和不止一个 class 项实施。我已经尝试了一些无济于事的方法,比如使用 class 来包含我的视图模型列表和一个整数来表示存储它们所需的页数。