另一個建議是創建將採取啓動和尾頁的參數定製的HtmlHelper。在助手,你應該做這樣的事情:
public static string Pager<TModel>(this HtmlHelper<TModel> html, Func<TModel, string> Prefix, int StartPage, int EndPage) where TModel : class
{
var builder = new StringBuilder();
for (var i = StartPage; i <= EndPage; i++)
builder.Append(html.Label(Prefix.Invoke(html.ViewData.Model) + i));
return builder.ToString();
}
測試(請注意,我用的MvcContrib TestControllerBuilder和RhinoMocks:
[Test]
public void Pager_should_be_ok()
{
var testBuilder = new TestControllerBuilder();
var controller = new TestController();
testBuilder.InitializeController(controller);
var viewData = MockRepository.GenerateStub<IViewDataContainer>();
viewData.ViewData = new ViewDataDictionary
{
Model = new TestObject { Key = "Test", Value = "Value" }
};
var viewContext = new ViewContext { RouteData = new RouteData(), RequestContext = controller.ControllerContext.RequestContext };
var html = new HtmlHelper<TestObject>(viewContext, viewData);
var output = html.Pager(x => x.Key, 1, 2);
Assert.That(output, Is.EqualTo("Test1Test2"));
}
然後在您的視圖,您可以使用此類似:
<%= Html.Pager(x => x.Controller, Model.StartPage, Model.EndPage) %>
所以這是避免你不想看到和這是可測試的意大利麪條代碼
而在主題命名的獎去.. *擊鼓聲* .....別人。 – 2009-11-06 12:57:24