如何在剃刀视图VB.net中显示模型对象属性

时间:2014-01-20 15:49:31

标签: xml asp.net-mvc vb.net linq razor

我正在使用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 +&gt;每次我都应该看到,例如产品代码?

1 个答案:

答案 0 :(得分:1)

在您的班级listOfProducts中声明您的媒体资源ProdSearchModel时,您未设置该媒体资源的类型:

Public Property listOfProducts = New List(Of returnedProduct)

这意味着您的媒体资源属于Object,因此Intellisense无法为您提供帮助。在运行时,它将被分配一个List(Of returnedProduct),这是您在视图中设置断点时所看到的。

您应该设置属性的类型,以便编译器和Intellisense了解它:

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

您的returnedProduct课程以及您从Xml阅读的方式也存在一些问题。我建议你快速查看this msdn link

要检索节点值,您可以将节点强制转换为所需的简单类型,也可以访问其value属性。 (如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

使用之前的更改,您应该在视图中使用Intellisense(假设视图中@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>

希望它有所帮助!