Anybody developping this project with VB NET ?

Jan 7, 2011 at 2:57 AM

Hi everybody

I am doing this project with VB net despite i am having a good understanding of C #

I have reached  page 35 without any trouble....

But at the page 35 The code does not display the 5 genres .

I believe i have done perfectly  the conversion verifying  it thrice !

This is what i am having

1)---Home Page : display as expected. only the number five is missing

2)--\Store page : Home Page plus

Browse Genres

Select From genres

*

*

*

*

*

 there s none of the five string(Rock Jazz, Pop, country, Disco.) on the Store page do not appear

I spent hours to verify reverify my code to find where is the mistake

But unfortunately i could not fix it

Please  help  !

Regards

Armand

 

------Extract from  View\Store\Index.aspx

 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h3>Browse Genres</h3>
    <p>Select from <%: Model.NumberOfGenres.ToString()%> genres:</p>
    <ul>
    <% For Each genreName As String In Model.Genres %>
        <li>
            <%:Html.ActionLink(genreName, "Browse", new with { key.genre = genreName })  %>
        </li>
    <% Next %>

</ul>
</asp:Content>

 

-----StoreController Class

Namespace MvcZikStore

    Public Class StoreController
        Inherits System.Web.Mvc.Controller

        'Get Store

        Public Function Index() As ActionResult

            ' Create a list of genres
            Dim genres = New List(Of String)
            genres.Add("Rock")
            genres.Add("Jazz")
            genres.Add("Country")
            genres.Add("Pop")
            genres.Add("Disco")

            ' Create our view model
            Dim viewModel = New StoreIndexViewModel()

            With viewModel
                .NumberOfGenres = genres.Count()
                .Genres = genres
            End With

            Return View(viewModel)
        End Function
        '
        ' GET: /Store/Browse
        Public Function Browse(ByVal genre As String) As String
            Dim message As String = HttpUtility.HtmlEncode("Store.Browse, Genre = " & genre)
            Return message
        End Function
        '
        ' GET: /Store/Details
        Public Function Details(ByVal id As Integer) As String
            Dim message As String = "Store.Details, ID = " & id
            Return message
        End Function


    End Class
End Namespace

Jan 14, 2011 at 5:47 AM

Hi Armand,

This is how I translated Jon's code to VB for the StoreController Index

        Function Index() As ActionResult

            ' Create list of Genres
            Dim genres As New List(Of String) From {"Rock", "Jazz", "Country", "Pop", "Disco"}

            ' Create ViewModel
            Dim viewModel As New StoreIndexViewModel With {.NumberOfGenres = genres.Count,
                                                           .Genres = genres}

            Return View(viewModel)

        End Function

This is the View aspx code

    <h3>Browse Genres</h3>

    <p>Select from <%: Model.NumberOfGenres%> genres:</p>

    <ul>
        <% For Each genreName As String In Model.Genres%>
            <li>
                <%: Html.ActionLink(genreName, "Browse", New With {.genre = genreName})%>
            </li>
        <% Next%>
    </ul>

 

Hope it works for you.

Cheers,

Gordon

 

Jan 27, 2011 at 8:21 PM

Hi Armand,
If you're still having problems please let me know as I have converted almost all of it to VB, including the use of the original SQL database with singular table names... thanks to Jon's help with the table issue.

There is one thing I'm still trying to figure out; I can't find the proper syntax to include the HTML helpers in a view; the following won't work so if anyone has any ideas I would sure appreciate it.

@ModelType IEnumerable(Of MvcMusicStore.Album)          'works fine

@Inherits MvcMusicStore.HtmlHelpers                                ' doesn't work - this or some way to call / use class needed at this point @using is used in C#

 

Coordinator
Jan 28, 2011 at 12:23 AM

I believe the correct keyword is @Imports. See this post on StackOverflow for some important things to watch for: http://stackoverflow.com/questions/4789578/namespace-reference-in-net-mvc3-razor-view-in-vb

Jan 28, 2011 at 3:56 AM

Thanks Jon,
I found that post right before you answered and that solved the imports part; I had tried imports but I had put it where your tutorial indicated.

I'm still having a problem getting the truncate function to work; one concern is that the way the function is shown on the page in the tutorial I don't see it sending three parameters back to the class. But when you type @Html.Truncate the IntelliSense gives the appearance that this is part of the album class so I'm not sure what the issue is.

Jan 31, 2011 at 8:52 AM

After reading a similar post about the truncate issue I revised the VB code as follows. Hope this helps anyone with this problem. Note that the imports statement has to be first.

@Imports MvcMusicStore.HtmlHelpers

@ModelType IEnumerable(Of MvcMusicStore.Album)
   
@Code
    ViewData("Title") = "Index"
End Code

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th></th>
        <th>
            Title
        </th>
        <th>
            Artist
        </th>
        <th>
            Genre
        </th>
    </tr>

@For Each item In Model
    @<tr>
        <td>
            @Html.ActionLink("Edit", "Edit", New With {.id = item.AlbumId}) |
            @Html.ActionLink("Details", "Details", New With {.id = item.AlbumId}) |
            @Html.ActionLink("Delete", "Delete", New With {.id = item.AlbumId})
        </td>
        <td>
            @Truncate(item.Title, 25)
        </td>
        <td>
            @Truncate(item.Artist.Name, 25)
        </td>
        <td>
            @item.Genre.Name
        </td>
    </tr>
Next
</table>

Mar 4, 2011 at 8:05 AM

Hi madison,

How did you implement HtmlHelpers.vb? Is it extension to HtmlHelpers class?  It gives me "Extension methods can only be defined in module" error.

I wrote this module but it did not help:

 

Imports System.Web.Mvc

Namespace MVCTest.Helpers

    Module HtmlHelpers
        <System.Runtime.CompilerServices.Extension()> _
        Public Function Truncate(ByVal helper As HtmlHelper, ByVal input As String, ByVal length As Integer) As String
            If input.Length <= length Then
                Return input
            Else
                Return input.Substring(0, length) + "..."
            End If
        End Function
    End Module
End Namespace

 

 

Mar 5, 2011 at 6:34 PM

Sorry, I should have added the function as well... it follows

*****

Imports System.Web.Mvc

Public Class HtmlHelpers

    Public Shared Function Truncate(ByVal input As String, ByVal length As Integer) As String
        If input.Length <= length Then
            Return input
        Else
            Return input.Substring(0, length) & "..."
        End If
    End Function
End Class

*****

Two things to note

When using VB the namespace is implied and should not be used when creating a class.

The tutorial is a little off; there should only be two parameters. Please note its use in the previous post above yours.

Jun 7, 2011 at 12:38 AM

Hi All,

I am new in this MVC method website.

I followed the tutorial and now I got stuck with the sample data provided to me. it was in C# and I have problem converting it.

I tried to convert it from some web based converter and it gave me 

Protected Overrides Sub Seed(context As MusicStoreEntities)
            Dim genres = New List(Of Genre)() From { _
             New Genre() With { _
               .Name = "Classical" _
             } _
            }

            Dim artists = New List(Of Artist)() From { _
             New Artist() With { _
               .Name = "Aaron Copland & London Symphony Orchestra" _
             } _
            }

            New List(Of Album)() From { _
             New Album() With { _
              .Title = "A Copland Celebration, Vol. I", _
              .Genre = genres.[Single](Function(g) g.Name = "Classical"), _
              .Price = 8.99D, _
              .Artist = artists.[Single](Function(a) a.Name = "Aaron Copland & London Symphony Orchestra"), _
              .AlbumArtUrl = "/Content/Images/placeholder.gif" _
             } _
            }.ForEach(Function(a) context.Albums.Add(a))

            Dim test As New List(Of Album)() From { _
             New Album() With { _
              .Title = "A Copland Celebration, Vol. I", _
              .Genre = genres.[Single](Function(g) g.Name = "Classical"), _
              .Price = 8.99D, _
              .Artist = artists.[Single](Function(a) a.Name = "Aaron Copland & London Symphony Orchestra"), _
              .AlbumArtUrl = "/Content/Images/placeholder.gif" _
             } _
            }

            For Each album As Album In test
                context.Albums.Add(album)
            Next

End Sub

Obviously it's not correct. then I tried modifying it to
 Dim test As New List(Of Album)() From { _
             New Album() With { _
              .Title = "A Copland Celebration, Vol. I", _
              .Genre = genres.[Single](Function(g) g.Name = "Classical"), _
              .Price = 8.99D, _
              .Artist = artists.[Single](Function(a) a.Name = "Aaron Copland & London Symphony Orchestra"), _
              .AlbumArtUrl = "/Content/Images/placeholder.gif" _
             } _
            }

            For Each album As Album In test
                context.Albums.Add(album)
            Next

but then and again. it does not work for me.
it did not give me the result when I go to /store page.

Please help me solve this syntax issue. Thank you very much guys




Coordinator
Jun 11, 2011 at 7:14 AM

Here's the general syntax:

Dim TempList = New List(Of Album)() From {
 New Album With {
	.Title = "The Best Of Men At Work", 
	.Genre = genres.Single(Function(g) g.Name = "Rock"), 
	.Price = 8.99D, 
	.Artist = artists.Single(Function(a) a.Name = "Men At Work"), 
	.AlbumArtUrl = "/Content/Images/placeholder.gif"},
 New Album With {
	.Title = "A Copland Celebration, Vol. I", 
	.Genre = genres.Single(Function(g) g.Name = "Classical"), 
	.Price = 8.99D, 
	.Artist = artists.Single(Function(a) a.Name = "Aaron Copland & London Symphony Orchestra"), 
	.AlbumArtUrl = "/Content/Images/placeholder.gif"}

TempList.ForEach(Function(a) context.Albums.Add(a))

You can download the completed VB.NET code from http://mvcmusicstore.codeplex.com/releases/view/64379 - grab MvcMusicStore-v3.0-VB.zip