简体   繁体   中英

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

I just started with learning 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) .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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