简体   繁体   English

ASP.NET MVC 视图内部视图

[英]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/modelshttp://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.您需要的是设置一些局部视图并在渲染汽车细节时使用它。

What is a partial view ?什么是局部视图

It's a view inside view.这是一个视图里面的视图。 You can have multiple partial views in a view file.一个视图文件中可以有多个局部视图

How to use?如何使用?

@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细节时显示一些与brandmodel相关的信息。

Assuming that:假如说:

  • the view model for http://localhost/brands/1 is CarBrandViewModel and http://localhost/brands/1的视图模型是CarBrandViewModel
  • the view model for http://localhost/models/2 is CarModelViewModel . http://localhost/models/2的视图模型是CarModelViewModel

Model模型

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的视图模型。

View看法

You need to define 3 view files:您需要定义 3 个视图文件:

  • the view file for car's detail eg CarDetail.cshtml ;汽车细节的视图文件,例如CarDetail.cshtml
  • the partial view for brand info eg Shared\\_CarBrandDetail.cshtml ;品牌信息的部分视图,例如Shared\\_CarBrandDetail.cshtml
  • the partial view for model info eg 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 -->

Note:笔记:

  • 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM