ASP MVC结合视图 [英] ASP MVC combining views

查看:38
本文介绍了ASP MVC结合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于ASP MVC开发仍然是新手.假设我有一个MusicController,它具有2个视图:List(显示所有艺术家的列表)和Highlights(显示3个具有所有详细信息的特定专辑).

Still new to ASP MVC development. Suppose I have a MusicController that has 2 views: List (which shows a list of all artists) and Highlights (which shows 3 specific albums with all details).

我现在想在一个页面上同时显示两个视图.1)最好的方法是什么?部分意见?我是否需要为同时显示两个局部视图的页面制作一个单独的控制器?2)假设我想根据所选艺术家来更改重点.如何将参数传递给带有高光的局部视图?

I now want to render both views on one page. 1) What is the best way to do this? Partial views? Do I need to make a separate controller for the page that shows both partial views? 2) Suppose I want to change the highlights based on the selected artist. How to pass that parameter to the partial view with highlights?

感谢给我一些线索!

推荐答案

部分视图确实是您想要的.您可能会遇到的麻烦是,有时很难制作一个既可以独立运行又可以嵌入另一个视图的局部视图.解决方案是创建一个单独的视图,其中包含所有必需的样板,然后调用(单个)局部视图.然后,局部视图适合在单独页面和组合页面中使用.

Partial views is indeed what you want. One bit of trouble that you might run into is that sometimes it's hard to make a partial view that works as both stand-alone and embedded in another view. The solution is to create a separate view that contains all the necessary boilerplate that then calls the (single) partial view. Then the partial views are suitable to be used in both the individual pages and the combined page.

您的汇总视图不需要单独的控制器,但是它将需要自己的操作.

You don't need a separate controller for your aggregate view, but it will need its own action.

这就是我提倡的结构:

MusicController 中:

public ActionResult List() {
  return View();
}
public ActionResult Highlights() {
  return View();
}
public ActionResult ListAndHighlights() {
  return View();
}

列表 Highlights 的视图如下所示:

<div class="anyNecessaryChrome">
  @RenderPartial( "ListPartial" )
</div>

然后 ListAndHighlights()看起来像这样:

<div class="listChrome">
  @RenderPartial( "ListPartial" )
</div>
<div class="highlightsChrome">
  @RenderPartial( "HighlightsPartial" )
</div>

如果要更新部分视图以响应客户端上的某些内容,则必须使用AJAX.

If you want the partial views to update in response to something on the client side, you'll have to use AJAX.

这篇关于ASP MVC结合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆