asp.net-mvc asp.net mvc - 将部分数据模型传递给部分视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8724847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net mvc - pass partial data model to partial view
提问by Nir
I wish to build a partial view that gets a model column and print it. Something like that:
我希望构建一个获取模型列并打印它的局部视图。类似的东西:
At the view:
在视图中:
@model IEnumerable<products_comparison.Models.Product>
@{
ViewBag.Title = "Index";
var Brand = (from r in Model
select r.Brand).Distinct();
}
<h2>
Index</h2>
@Html.RenderPartial("_DisplayAttribute",Brand)
And at the partial view:
在局部视图中:
<table>
<tr>
<th>
Brand
</th>
</tr>
@foreach (var row in Model)
{
<tr>
<td>
@Html.DisplayFor(r => row)
</td>
</tr>
}
</table>
There are a few problems I run into:
我遇到了几个问题:
- The compiler doesnt allow me to send Barnd to the partial view.
- If you look at the partial view code you will see the word Brand, which is the column name. I dont wish to hard-coded the word "Brand" in the partial view, instead I like that the column name will be there.
- In the partial view I need to put @model products_comparison.Models.Product, but I dont want to send the hole table. I want to send only one column - But I dont know what to put there..
- 编译器不允许我将 Barnd 发送到局部视图。
- 如果您查看部分视图代码,您将看到 Brand 一词,即列名。我不希望在局部视图中硬编码“品牌”这个词,相反我喜欢列名将在那里。
- 在局部视图中我需要放置@model products_comparison.Models.Product,但我不想发送孔表。我只想发送一栏 - 但我不知道该放什么..
Thanks!
谢谢!
EDIT:
编辑:
Just to clear one thing, I want that the view will call the same partial view for each column in the table(for most of the columns in the table anyway) and each time I'll send a different column(distinct value column to be exact).
只是为了清除一件事,我希望视图将为表中的每一列调用相同的局部视图(无论如何对于表中的大多数列),并且每次我都会发送一个不同的列(不同的值列是精确的)。
回答by Darin Dimitrov
Start by refactoring and putting the right logic into the right place. This LINQ query has strictly nothing to do in a view. A view is not supposed to do any LINQ queries or whatever to pull data. A view is supposed to work with data that it is passed to it from the controller action under the form of a view model. A controller action builds and passes an adapted view model that you define for the view.
首先重构并将正确的逻辑放在正确的位置。这个 LINQ 查询与视图无关。视图不应该执行任何 LINQ 查询或任何提取数据的操作。视图应该以视图模型的形式处理从控制器操作传递给它的数据。控制器操作构建并传递您为视图定义的适配视图模型。
So as always you start by defining a view model that will be adapted to the requirements of your view:
因此,与往常一样,您首先定义一个将适应您的视图要求的视图模型:
public class MyViewModel
{
public IEnumerable<Brand> Brands { get; set; }
}
then you write a controller action that will populate this view model and pass it to the view:
然后你编写一个控制器动作来填充这个视图模型并将它传递给视图:
public ActionResult Foo()
{
IEnumerable<products_comparison.Models.Product> products = ...
var model = new MyViewModel
{
Brands = (from r in Model select r.Brand).Distinct()
};
return View(model);
}
then a view:
然后是一个视图:
@model MyViewModel
<table>
<tr>
<th>
Brand
</th>
</tr>
@Html.DisplayFor(x => x.Brands)
</table>
and finally you could define a corresponding display template which will automatically be rendered for each element of the Brandscollection of your view model (~/Views/Shared/DisplayTemplates/Brand.cshtml):
最后,您可以定义一个相应的显示模板,该模板将自动Brands为您的视图模型 ( ~/Views/Shared/DisplayTemplates/Brand.cshtml)集合的每个元素呈现:
@model Brand
<tr>
<td>
@Html.DisplayForModel()
</td>
</tr>
回答by Alexandros B
For 1try changing @Html.RenderPartial("_DisplayAttribute",Brand)to @Html.Partial("_DisplayAttribute",Brand)
对于1尝试更改@Html.RenderPartial("_DisplayAttribute",Brand)为@Html.Partial("_DisplayAttribute",Brand)
You will also need to specify the model in the partial view like @model products_comparison.Models.Brandor something like it
您还需要在局部视图中指定模型,例如@model products_comparison.Models.Brand或类似的东西
Also please clarify 2 & 3 as they are not clear what you want
另外请澄清 2 和 3,因为他们不清楚你想要什么

![spring 临时上传位置[/tmp/tomcat.4296537502689403143.5000/work/Tomcat/localhost/ROOT]无效](/res/img/loading.gif)