Just Learning ASP.NET, VB.Net, MVC right now.
My intention is to pass a model to a view.
I have successful test code for a simple string case.
Where I think I am stumbling today is in that the current viewmodel is a list of objects.
The error I get is “The model item passed into the dictionary is of type ‘MVC_Test1.Geocode_Google_Results[]’, but this dictionary requires a model item of type ‘MVC_Test1.Geocode_Google_Results’.”
So with my limited understanding, it appears that I am passing an object list(as intended) but the view model typeing is the simple object type that was used to create the list.
In a number of examples, the responses were in C#. These focused upon a For Each approach (which was my first notion of how to make this work).
I have tried this but ended up with object type errors in the For Each … AS object clause.
I found an www example which indicated that it should be approached as an array as in:
For I as integer = 0 to Model.Results.Count
This has allowed me to build the code but then results in the object type error mentioned above at runtime rather than at build.
Continueing reading, there are several topics which seem to invoke @model IEnumerable as an approach. This just makes the viewdata and model objects undefined when done as:
@Modeltype IEnumberable MVC_Test1.Geocode_Google_Results
or
@Model IEnumberable MVC_Test1.Geocode_Google_Results
I suspect that there is something simple/fundimental that I should know to make this all work. This just seems to straight forward of a design problem to not have a well constructed answer.
If the answer could be explained in the context of VB.net, It would be more helpful to me. I do use one of those web C# to VB.net converters to translate at times so any response is better than none.
Thanks,
Details below:
I have a simple class:
Public Class Geocode_Google_Results
Public Property Results As New List(Of ManyGeocodeCoordinates)
'Methods
'Add to list
Public Sub AddResults(Address, Latitude, Longitude)
Results.Add(New ManyGeocodeCoordinates() With {.Address = Address, .latitude = Latitude, .longitude = Longitude})
End Sub
End Class
Public Class ManyGeocodeCoordinates
Public Property Address As String
Public Property latitude As Double
Public Property longitude As Double
End Class
I then populate with some data:
For x As Integer = 1 To resultCount
ManyResults(x) = New Geocode_Google_Results
ManyResults(x).AddResults(results.Elements("result").Elements("formatted_address").Value, results.Elements("result").Elements("geometry").Elements("location").Elements("lat").Value, results.Elements("result").Elements("geometry").Elements("location").Elements("lng").Value)
Next
I then pass along to the View:
ElseIf resultCount = 1 Then
' Send the user to the results page...Default View Page
Return View(ManyResults)
I then have a test view:
@Modeltype MVC_Test1.Geocode_Google_Results
@Code
ViewData("Title") = "httptest"
End Code
<h2>httptest</h2>
Default single address view
@model.Results(0).Address <br />
@model.results(0).latitude <br />
@model.results(0).longitude <br />
Upon running this in IE, I get the aformentioned error regarding the object list versus the object for the model type.
After continued reading and experimenting here is what I found.
A viewmodel (class) is a singular object. When I populated the instance of class with data, I did so using an array notation undoubtedly drawn from the object array context. However, the class is not an array of objects. This function was served by the property that was a list of objects.
So assigning the data as Manyresults(x) was incorrect.
The data needed to be assigned as
Simlarly, in the view the model data is accessed with the form:
In my example, when multiple results are returned, the Model.Results(n).Address would be used where n is the result dimension that you are looking for. Model.results.count would be used to determine how many results there are in the view model.