Music Store 2 Tutorial - VB Conversion

2019-06-07 19:47发布

问题:

I am new to asp.net and have been working through the tutorial - up to lesson 10, however I cannot get the partial view to work.

Controller:- ' ' GET: /ShoppingCart/CartSummary

    <ChildActionOnly()>
    Public Function CartSummary() As ActionResult
        Dim cart = ShoppingCart.GetCart(Me.HttpContext)

        ViewData("CartCount") = cart.GetCount()

        Return PartialView("CartSummary")
    End Function

Views:- CartSummary:-

@modeltype TEST.Cart

@Html.ActionLink("Cart (" & ViewData("CartCount") & ")", _
                      "Index", _
                      "ShoppingCart", _
                      New With {.id = "cart.GetCount"})

_Layout:-

<!DOCTYPE html>
<html>
<head>
    <title>@ViewData("Title")</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>

<body>

    <div id="header">
        <h1>   ASP.NET MVC MUSIC STORE</h1>
        <ul id="navlist">
            <li class="first"><a href="@Url.Content("~")" id="current">Home</a></li>
            <li><a href="@Url.Content("~/Store/")">Store</a></li>
            <li>@Html.RenderAction("CartSummary", "ShoppingCart")</li>
            <li><a href="@url.Content("~/StoreManager/")">Admin</a></li>
        </ul>
    </div>
    <div id="main">
           @RenderBody()
    </div>
</body>
</html>

The error returned is "Expression does not produce a value". I can remove the tag and then display the CartSummary.

Thanks for any help.

回答1:

If you want to use the Html.RenderAction helper here's the correct syntax:

@Code
    @Html.RenderAction("CartSummary", "ShoppingCart")
End Code

Or if you prefer a one-liner:

@Code @Html.RenderAction("CartSummary", "ShoppingCart") End Code

or if you want to use the the Html.Action helper this is the syntax:

@Html.Action("CartSummary", "ShoppingCart")

Contrast those two with their C# equivalents:

@{Html.RenderAction("CartSummary", "ShoppingCart");}
@Html.Action("CartSummary", "ShoppingCart")

As a VB.NET guy you might find the following article useful.