[英]ASP.NET MVC View inside view
I'm new to ASP.NET MVC and I would like to ask how to make "cascading" view (I don't know if this is the correct term).我是 ASP.NET MVC 的新手,我想问一下如何制作“级联”视图(我不知道这是否正确)。
Let's see: I have a view that returns car brands, let's say in http://localhost/brand.让我们看看:我有一个返回汽车品牌的视图,假设在 http://localhost/brand 中。
If I go to http://localhost/brand/1
, I get the details of the brand with code 1如果我去
http://localhost/brand/1
,我会用代码 1 获得品牌的详细信息
I can create another view with the models of each brand, let's say http://localhost/models
and http://localhost/models/2
I get the model details我可以用每个品牌的模型创建另一个视图,假设
http://localhost/models
和http://localhost/models/2
我得到模型详细信息
But what I want is to go to http://localhost/car/1/2
and get the some details of that model.但我想要的是访问
http://localhost/car/1/2
并获取该模型的一些详细信息。 But must be a relation between brand and model但一定是品牌和型号的关系
In ASP.NET MVC, how can I get this view inside the view ?在 ASP.NET MVC 中,如何在视图中获取此视图?
Thanks in advance提前致谢
What you need is setting up some partial views and use it when rendering the details of a car.您需要的是设置一些局部视图并在渲染汽车细节时使用它。
It's a view inside view.这是一个视图里面的视图。 You can have multiple partial views in a view file.
一个视图文件中可以有多个局部视图。
@Html.Partial("{your view name}", "{your model}")
So back to your example: you want to show some information related to brand
and model
when rendering the details of a car
.回到你的例子:你想在渲染
car
细节时显示一些与brand
和model
相关的信息。
Assuming that:假如说:
http://localhost/brands/1
is CarBrandViewModel
and http://localhost/brands/1
的视图模型是CarBrandViewModel
和http://localhost/models/2
is CarModelViewModel
. http://localhost/models/2
的视图模型是CarModelViewModel
。For your car
detail page, you need to define a CarDetailViewModel
:对于您的
car
详细信息页面,您需要定义一个CarDetailViewModel
:
public class CarViewModel
{
public CarBrandViewModel CarBrand { get; set; }
public CarModelViewModel CarModel { get; set; }
/* some other fields for your car */
}
And this CarViewModel
will be the view model of http://localhost/car/1/2
.而这个
CarViewModel
将是http://localhost/car/1/2
的视图模型。
You need to define 3 view files:您需要定义 3 个视图文件:
CarDetail.cshtml
;CarDetail.cshtml
;Shared\\_CarBrandDetail.cshtml
;Shared\\_CarBrandDetail.cshtml
;Shared\\_CarModelDetail.cshtml
Shared\\_CarModelDetail.cshtml
The _CarBrandDetail.cshtml
file should accepts a CarBrandViewModel
object as the model: _CarBrandDetail.cshtml
文件应该接受一个CarBrandViewModel
对象作为模型:
// in your _CarBrandDetail.cshtml...
@model CarBrandViewModel
<!-- your code for rendering brand information ->
Define the similar thing in _CarModelDetail.cshtml
.在
_CarModelDetail.cshtml
定义类似的东西。 This time you should set:这次你应该设置:
@model CarModelViewModel
Now, in CarDetail.cshtml
:现在,在
CarDetail.cshtml
:
// in your CarDetail.cshtml...
@model CarViewModel
@* render your brand's details *@
@{
Html.RenderPartial("_CarBrandDetail", Model.CarBrand);
}
@* render your model's details *@
@{
Html.RenderPartial("_CarModelDetail", Model.CarModel);
}
<!-- your code for rendering other details of the car -->
A @{
}
pair is required to surround the Html.RenderPartial();
需要
@{
}
对来包围Html.RenderPartial();
call.称呼。
It's not necessary to put all the partial views under Shared
folder.没有必要把所有的局部视图都放在
Shared
文件夹下。 However, if you put the partial views somewhere else under Views
, you need to change the file path when calling Html.RenderPartial()
.但是,如果将局部视图放在
Views
下的其他位置,则需要在调用Html.RenderPartial()
时更改文件路径。
You may want to reuse your _CarBrandDetail
in http://localhost/brands/1
.您可能希望在
http://localhost/brands/1
重用您的_CarBrandDetail
。 In this case you can make a view for the brand/{id}
page and then call the _CarBrandDetail
partial view.在这种情况下,您可以为
brand/{id}
页面创建一个视图,然后调用_CarBrandDetail
部分视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.