2014-01-20 34 views
1

我的工作在Razor視圖VB.net模型對象的屬性ASP MVC 4 VB.net如何顯示

我有一個模型:

Public Class ProdSearchModel 

    Class returnedProduct 

     Property advertiser_id 
     Property advertiser_name 
     Property advertiser_category 
     Property buy_url 
     Property currency 
     Property description 
     Property image_url 
     Property name 
     Property price 
     Property sale_price 

    End Class 

    Public Property query As String 
    Public Property returnedXML As XElement 
    Public Property xmlString As String 
    Public Property listOfProducts = New List(Of returnedProduct) 

    Sub getXML() 

       Using client As New WebClient 

       ' Download data as byte array. 
       Dim arr = client.DownloadString("https://remoteserver.com/product-search?keywords=" & System.Web.HttpUtility.UrlEncode(query)) 

       returnedXML = XElement.Parse(arr) 

       For Each product In returnedXML...<product> 
        Dim currentProduct As New returnedProduct 

       currentProduct.advertiser_id = product...<advertiser-id> 
       currentProduct.advertiser_name = product...<advertiser-name> 
       currentProduct.advertiser_category = product...<advertiser-category> 
       currentProduct.buy_url = product...<buy-url> 
       currentProduct.currency = product...<currency> 
       currentProduct.description = product...<description> 
       currentProduct.image_url = product...<image-url> 
       currentProduct.name = product...<name> 
       currentProduct.price = product...<price> 
       currentProduct.sale_price = product...<sale-price> 

       listOfProducts.add(currentProduct) 
      Next 

     End Using 

    End Sub 


End Class 

這拉低一些XML

<advertiser-id>531450</advertiser-id> 
<advertiser-name>Cell Phone Shop</advertiser-name> ... 

這是所有罰款...

我的控制器看起來是這樣的:

Public Class ProdSearchController 
    Inherits System.Web.Mvc.Controller 

    ' 
    ' GET: /ProdSearch 

    Function Index(query As String) As ActionResult 

     Dim model As New ProdSearchModel With {.query = query} 
     'model.returnedXML will have the response 
     model.getXML() 

     Return View(model) 
    End Function 

End Class 

和視圖看起來是這樣的:

@ModelType matrix.ProdSearchModel 
@Code 
    Layout = Nothing 

End Code 

<!DOCTYPE html> 
<html> 
<head runat="server"> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
</head> 
<body> 
    <div> 
    @For Each product In Model.listOfProducts 

    @product.advertiser_category 

    Next 

    <hr /> 

     @*Html.Raw(Model.returnedXML) *@ 

    </div> 
</body> 
</html> 

如果我轉儲XML 「Model.returnedXML」 它都在那裏。

但是,如何引用「listOfProducts」中的每個項目?

也沒有對@product沒有智能感知:

enter image description here

我只是等的ToString,而不是實際的屬性,但如果我在調試模式下運行,我可以看到的數據結構是存在的。

enter image description here

此刻,我只是得到d__aSystem.Xml.Linq.XContainer + d__aSystem.Xml.Linq.XContainer +每一次我看到例如>產品代碼?

回答

1

當你ProdSearchModel類聲明你的財產listOfProducts,你是不是設置屬性的類型:

Public Property listOfProducts = New List(Of returnedProduct) 

這意味着你的財產將是Object類型,因此智能感知將不能夠幫助你。在運行時,它將被分配一個List(Of returnedProduct),這是您在視圖中設置斷點時看到的內容。

你應該設置你的屬性的類型,所以編譯器和智能感知了解它:

Public Property listOfProducts As List(Of returnedProduct) = New List(Of returnedProduct) 

也有一些問題,你returnedProduct類,你是從XML閱讀方式。我建議你快速瀏覽一下this msdn link

對於檢索節點值,可以將節點轉換爲所需的簡單類型或訪問其值屬性。 (如msdn here中所述)。 通過定義屬性類型,你returnedProduct類,像下面開始(我猜的類型,使用,使你最感的那些):

Class returnedProduct 
    Property advertiser_id As Integer 
    Property advertiser_name As String 
    Property advertiser_category As String 
    Property buy_url As String 
    Property currency As String 
    Property description As String 
    Property image_url As String 
    Property name As String 
    Property price As Decimal 
    Property sale_price As Decimal 
End Class 

然後從節點填充性能,使用Value屬性的示例。由於性能Object類型不,值將被很好地轉化爲預期的類型:

currentProduct.advertiser_id = product...<advertiser-id>.Value 
currentProduct.advertiser_name = product...<advertiser-name>.Value 
currentProduct.advertiser_category = product...<advertiser-category>.Value 
currentProduct.buy_url = product...<buy-url>.Value 
currentProduct.currency = product...<currency>.Value 
currentProduct.description = product...<description>.Value 
currentProduct.image_url = product...<image-url>.Value 
currentProduct.name = product...<name>.Value 
currentProduct.price = product...<price>.Value 
currentProduct.sale_price = product...<sale-price>.Value 

與以往的變化,你應該得到智能感知您的視圖中工作(假設@ModelType聲明是正確的視圖) 。您現在也應該獲得正確的輸出,returnedProduct中的屬性已正確定義並填充。

  • 此前,您將它們作爲Object,並且未針對每個節點錯誤地填充Linq.Xml對象,而不是節點值。 Razor在每個屬性上調用ToString方法,該方法返回運行時類型名稱。

例如,您可以創建一個非常簡單的視圖渲染表與一些產品領域:

<table> 
    <thead> 
     <tr> 
      <th>advertiser_id</th> 
      <th>advertiser_name</th> 
      <th>advertiser_category</th> 
      <th>buy_url</th> 
      <th>currency</th> 
      <th>price</th> 
      <th>description</th> 
     </tr> 
    </thead> 
    <tbody> 
    @For Each product In Model.listOfProducts 
     @<tr> 
      <td>@product.advertiser_id</td> 
      <td>@product.advertiser_name</td> 
      <td>@product.advertiser_category</td> 
      <td>@product.buy_url</td> 
      <td>@product.currency</td> 
      <td>@product.price</td> 
      <td>@product.description</td> 
     </tr> 
    Next 
    </tbody> 
</table> 
<hr /> 
<pre> 
@Model.returnedXML 
</pre> 

希望它能幫助!