2017-06-06 19 views
0

我想連接兩個字段並列出數據庫類的所有項目。使用Entity frameworkASP.Net MVC的列表方法,爲什麼我不能這樣做?實體框架5:連接兩個字段

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.FName + " " + item.Lname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Address) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Email) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) | 
      @Html.ActionLink("Accounts", "Accounts", new { id=item.CustomerID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID }) 
     </td> 
    </tr> 
} 

我得到以下錯誤:

Compilation Error 

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1061: 'Customer' does not contain a definition for 'Lname' and no extension method 'Lname' accepting a first argument of type 'Customer' could be found (are you missing a using directive or an assembly reference?) 

如何解決此問題?

回答

1

簡單的解決方法:

@Html.DisplayFor(modelItem => item.FName) @Html.DisplayFor(modelItem => item.LName) 

或者,一個屬性添加到您的模型:

public string FullName { get { return FName + " " + LName; } } 

然後執行:

@Html.DisplayFor(modelItem => item.FullName) 

附加說明:

經進一步檢查你的錯誤,我懷疑你錯打Lname - 也許你的意思是LName?無論如何,以這種方式使用級聯將不起作用,並將導致此錯誤,而不是:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.