且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asp.net MVC的行动和观点的问题

更新时间:2023-02-16 11:33:25

使用一个视图模型来收集只是你想在视图中显示的信息。在你的选择逻辑,选择进入***从类别和协会的相关部分属性视图模型。你当然可以有多个参数的方法。提供额外的参数给路由值在ActionLink的形式或辅助方法,它应该构造URL正确引用你的行动。注意:您将需要改变模型的类型,在视图以及以匹配从操作。

Use a viewmodel to collect just the information that you want to show in your view. In your selection logic, select into the viewmodel the top-level attributes from the category and the relevant parts of the association. You can certainly have more than one parameter to the method. Supply the extra parameters to the route values in your ActionLink or Form helper methods and it should construct the Url properly to reference your action. Note you will need to change the type of the model in the view as well to match that from the action.

public ActionResult Catalog(string id, int? page)
{
    page = page ?? 1;
    var category = pe.Categories.SingleOrDefault(cat => cat.CategoryName == id);
    var model = new CatalogViewModel
                {
                       ID = category.ID,
                       Name = category.CategoryName,
                       Subcategories = category.Subcategories,
                       Products = category.Products.ToPagedList( page, PageSize )
                };

    return View(model);
}

查看(样品)

@if (Model.Products.Page < Model.Products.Pages)
{
    @Html.ActionLink( "Next",
                      "catalog",
                      new { id = Model.ID, page = Model.Products.Page + 1 } ) 
}