简体   繁体   English

来自其他模型的MVC显示属性

[英]MVC Display attribute from another model

so my view starts off like this 所以我的观点是这样开始的

@model ApplicationName.Models.A

and takes in the my database elements from A so if i were to use @Html.DisplayFor(model => model.Whatever) it will display that element from the database in my view. 并从A接收我的数据库元素,因此,如果我要使用@ Html.DisplayFor(model => model.What),它将在我的视图中显示数据库中的该元素。

However i have another attribute that i want to also display on this same page/view. 但是,我还有另一个要显示在同一页面/视图上的属性。 But it is in Models.B 但这是在Models.B中

So i need to end up with this 所以我需要结束这个

@Html.DisplayFor(model => model.WhateverFromB) @ Html.DisplayFor(model => model.WhateverFromB)

all help greatly appreciated 所有帮助极大的赞赏

You should use a view model which encapsulates both an A and a B . 您应该使用封装AB的视图模型。 For example: 例如:

class MyViewModel
{
    public ApplicationName.Models.A A {get; set;}
    public ApplicationName.Models.B B {get; set;}
}

Then pass this single view model to the controller. 然后将此单视图模型传递给控制器​​。 You would then of course access your properties like: 然后,您当然可以访问您的属性,例如:

@Html.DisplayFor(m => m.A.Whatever)
@Html.DisplayFor(m => m.B.WhateverFromB)

Similarly if you do not need every property from A and B , or the semantic difference between the two database objects is not important to the view, you might consider doing the following instead: 同样,如果您不需要AB每个属性,或者两个数据库对象之间的语义差异对于视图而言并不重要,则可以考虑执行以下操作:

class MyViewModel
{
    public object Whatever {get; set;}
    public object WhateverFromB {get; set;}
}

Populate the individual properties of the view model and use accordingly. 填充视图模型的各个属性并相应地使用。 You can of course use a combination of the two, including a full A along with a WhateverFromB . 当然,您可以结合使用两者,包括完整的A以及WhateverFromB

You should use a ViewModel for this. 您应该为此使用ViewModel When you require more than one model in your view this is a good way to go. 当您在视图中需要多个模型时,这是一个不错的方法。 Include what you need from the Models you are using in the ViewModel class and reference it in your view instead of the Model. 在ViewModel类中包含您正在使用的模型中需要的内容,并在视图中而不是模型中引用它。

This will get you going with ViewModels http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-3 这将帮助您使用ViewModels http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-3

ViewBags and ViewData will work but there are appropriate times to use each. ViewBagsViewData可以使用,但是有适当的时间使用它们。 http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-应用程序

Realistically you should create a ViewModel which contains all the properties you need and you can populate that ViewModel from multiple Models. 实际上,您应该创建一个包含所需所有属性的ViewModel,然后可以从多个Model中填充该ViewModel。 Then use the ViewModel as the 'model' in the MVC page. 然后在MVC页面中将ViewModel用作“模型”。

However to answer your question. 但是要回答你的问题。 Just write an additional Html.Display entry 只需编写一个额外的Html.Display条目

In your controller 在您的控制器中

ViewBag.WhateverFromB = b_obj;

and then in your view 然后在您看来

@Html.Display(ViewBag.WhateverFromB)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 MVC model 读取显示属性 - Read the display attribute from MVC model ASP Net MVC-具有模型属性引用另一个模型属性 - asp net mvc - have a model attribute reference another model attribute 在 mvc asp.net 应用程序中通过其主键与 ViewBag 和 asp-items 显示来自 model 的属性 - display attribute from model by its primary key with ViewBag and asp-items in mvc asp.net application 具有来自模型属性的data-属性的MVC编辑器 - MVC Editor with data- attribute from model attribute ASP.NET MVC View需要显示特定模型的数据,并同时为另一个模型获取输入数据 - ASP.NET MVC View needs to display data from a certain model and at the same time take input data for another model 模型中的Display属性是否违反视图和模型中的关注点分离 - Does the Display attribute in the model violate separation of concerns from view and model mvc DisplayName或Display(Name = ...)取决于另一个模型属性 - mvc DisplayName or Display(Name=…) depending on another model property MVC 3在另一个控制器中显示数据 - MVC 3 display data from a controller in another 从控制器获取MVC2模型属性值 - Get MVC2 model attribute value from Controller C#MVC 5基于ID显示列表中的模型文本 - c# mvc 5 display model text from list based on id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM