简体   繁体   English

为什么我的模型属性未反映在剃刀的自动生成的视图中? (MVC 4)

[英]Why is my model property not reflected in a razor's auto generated view ? (MVC 4)

I just started with learning MVC. 我刚开始学习MVC。 So, here is my model class 所以,这是我的模型课

public class Patient : iPatient, iPerson
{
    public string name { get; set; }
    public int age { get; set; }
    public DateTime dateOfBirth { get; set; }
    public Address address { get; set; }
    public string bloodGroup { get; set; }
}

So, when I right click in my action method of the controller and add view, the scaffolding produces some view like this: 因此,当我右键单击控制器的操作方法并添加视图时,脚手架将产生如下视图:

<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.age)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.dateOfBirth)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.bloodGroup)
    </th>
    <th></th>
</tr>

It does not produce something like @Html.DisplayNameFor(model=>model.Address.City) . 它不会产生类似@Html.DisplayNameFor(model=>model.Address.City)

Why is this behaviour ? 为什么会这样呢?

Do I need to manually add this in the view or is there any work around for this? 我需要在视图中手动添加它还是对此有任何解决方法?

Thanks in advance for your answer. 预先感谢您的回答。

Someone can correct me if I'm wrong, but I don't believe the auto scaffolding will do anything with sub object types, it just scaffolds the basic types. 如果我错了,有人可以纠正我,但是我不相信自动脚手架可以对子对象类型做任何事情,它只是脚手架上的基本类型。 You're on your own to add them yourself, although it is pretty easy. 您可以自己添加自己,尽管很容易。

One method you could use, is create a Strongly Typed partial view of Address. 您可以使用的一种方法是创建地址的强类型局部视图。

Then in your code for this view all you would have to add is 然后,在此视图的代码中,您需要添加的只是

@Html.Partial(Model.Address)

and all your address fields that are located in the partial view would show up. 就会显示位于部分视图中的所有地址字段。

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

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