簡體   English   中英

為什么我的模型屬性未反映在剃刀的自動生成的視圖中? (MVC 4)

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

我剛開始學習MVC。 所以,這是我的模型課

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; }
}

因此,當我右鍵單擊控制器的操作方法並添加視圖時,腳手架將產生如下視圖:

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

它不會產生類似@Html.DisplayNameFor(model=>model.Address.City)

為什么會這樣呢?

我需要在視圖中手動添加它還是對此有任何解決方法?

預先感謝您的回答。

如果我錯了,有人可以糾正我,但是我不相信自動腳手架可以對子對象類型做任何事情,它只是腳手架上的基本類型。 您可以自己添加自己,盡管很容易。

您可以使用的一種方法是創建地址的強類型局部視圖。

然后,在此視圖的代碼中,您需要添加的只是

@Html.Partial(Model.Address)

就會顯示位於部分視圖中的所有地址字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM