你们如何更新(让我们说)控制器动作中的两个部分进入视图?例如,您有一个左侧导航部分用于链接,右侧是您的主要内容。您的主要内容具有绑定的ViewModel。你们的导航页面有一个单独的ViewModel吗?如果是这样,那么每次具有主内容视图模型和左侧导航ViewModel时,您是否创建了更大的ViewModel?
答案 0 :(得分:0)
我会为导航页面(例如CategoryListViewModel
)创建一个ViewModel,为内容页面创建一个ViewModel(例如ProductListViewModel
)。然后在基本控制器中加载CategoryListViewModel
:
public abstract class BaseController : Controller
{
var _categoryRepository = new CategoryRepository();
protected BaseController()
{
ViewData["Categories"] = _categoryRepository.GetCategories();
}
}
public class ProductsController : BaseController
{
var _productRepository = new ProductRepository();
public ActionResult Index()
{
return View(_productRepository.GetProducts());
}
}
然后在MasterPage
中阅读ViewData
中的类别,并在内容页面中阅读Model
上ViewPage
的产品。
答案 1 :(得分:0)
答案 2 :(得分:-1)
实际上我最终还是躲到了this solution。谢谢大家的意见!