I have two simple viewmodels which are related. I can query the data via an linq include statement, but when i inject it into the viewmodel the icollection is nothing?
Viewmodels:
Public Class EventViewModel
Public Property EVENTID As Integer
Public Property TITLE As String
Public Overridable Property USERS() As ICollection(Of UserViewmodel)
End Class
Public Class UserViewModel
Public Property USERID As Integer
Public Property EVENTID As Integer
Public Property NAME As String
End Class
Query:
Dim dataObject As EVENTTABLE = db.EVENTTABLE.Include("USERTABLE").Single(Function(c) c.EVENTID= "1")
Users are in the Object!
Injection:
viewModel.InjectFrom(dataObject)
Users are Nothing?
Ok figured it out with help from this question: How to map lists with ValueInjector
I needed to query the USERTABLE into a list and inject it like stated below:
Query:
Dim dataSubObject = (From ....).toList()
Injection:
Dim viewModel = New EventViewModel
viewModel.USERS= dataSubObject.Select(Function(x) New ImageViewModel().InjectFrom(x)).Cast(Of ImageViewModel)().ToList()
Related
I have a query which selects a list of subrep. The query looks like this:
Public Async Function ReturnSubRepsAsync(user As BaseUser) As Task(Of IList(Of SubRep)) Implements IGeneralAccountManagementRepository.ReturnSubRepsAsync
'The entity type SubRep is not part of the model for the current context.
Using webDataDBContext As New WebDataEntities()
'So here it just has an SQL query which it will execute when requested
Dim subReps = webDataDBContext.Subreps.Where(Function(x) x.CID = user.CID)
If (String.IsNullOrEmpty(user.Password) = False) Then
'Add another where onto the SQL query
subReps = subReps.Where(Function(x) x.HASH_PASSWORD = user.PasswordHash)
End If
If (String.IsNullOrEmpty(user.Username) = False) Then
'Add another where onto the SQL query
subReps = subReps.Where(Function(x) x.username = user.Username)
End If
'Execute the query
Return Await subReps.ToListAsync()
End Using
End Function
Now due to entity framework database not including validation such as MaxLength on strings, I decided to follow this tutorial: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation
And add another partial public class which uses a MetaDataType to implement the validation. Now during debugging, I thought it was an issue with a column so I decided to comment out all of the code inside my separate partial class to test if it works, so I'm not left with an empty husk of a partial class. It has not made a difference, it still errors on the first where of my query.
My entity framework generated class looks like this:
Partial Public Class Subrep
Public Property id As Integer
Public Property CID As String
Public Property username As String
Public Property LastName As String
Public Property FirstName As String
Public Property MIddleInitial As String
Public Property Address As String
Public Property Address2 As String
Public Property City As String
Public Property State As String
Public Property Zip As String
Public Property HomePhone As String
Public Property EmailAddress As String
Public Property InActive As String
Public Property CellPhone As String
Public Property StartDate As String
Public Property AuditName As String
Public Property Background As String
Public Property FDCPA As String
Public Property Choicepoint As String
Public Property Badge As String
Public Property ConfAgree As String
Public Property NCCI_OK As String
Public Property InactiveRepNo As String
Public Property Approved As String
Public Property DateApproved As String
Public Property Denied As String
Public Property DateDenied As String
Public Property DeniedNotes As String
Public Property DOB As String
Public Property SSN As String
Public Property MegansLaw As String
Public Property MASTER As String
Public Property VACATION As String
Public Property FC_DEFAULT_FEE As Nullable(Of Decimal)
Public Property LM_DEFAULT_FEE As Nullable(Of Decimal)
Public Property MAS90 As String
Public Property SOS As String
Public Property HASH_PASSWORD As String
Public Property changepass As String
Public Property NOTIFICATION_OFF As String
Public Property Capacity As String
Public Property internalqc As String
Public Property New_Rep As String
Public Property Stand_By As String
Public Property Vacation_Start_date As Nullable(Of Date)
Public Property Vacation_End_date As Nullable(Of Date)
Public Property sub_coreIP As String
Public Property rating As String
Public Property date_last_updated As Nullable(Of Date)
End Class
And my separate partial class which is supposed to be used for validation looks like this:
Partial Public Class SubRep
End Class
So even though it's empty I get the error listed in the title. I have gotten this to work with multiple entities perfectly fine.
Just wow.
So with visual studio intellisense if you press F12 to try to find references it highlighted both partial classes Subrep and SubRep so that would lead you to believe that both were associated with one another. Turns out that because of the uppercase R in the second partial class SubRep, it's not associated with the model and therefore needs to match the case of the entity Subrep. Like this:
Partial Public Class Subrep
End Class
What's even weirder is that the Property in my DBContext uses an uppercase SubRep. Like this:
Public Overridable Property Subreps() As DbSet(Of SubRep)
my question is how serializer the next string Json:
data:
{
"Customers":[{"**PhoneNumber**":"123123123"},{"**PhoneNumber**":"321321321"}],
"Expand":false,
"Include":false
}
I to have doubt, because in the string JSon I have 2 equal labels, therefore i not know as create my class with the same labels, i'm using VB.net.
My Classes in VB.net
Public Class Customers
Public as customer1
Public expand as boolean
Public include as boolean
End Class
Public Class customers1
Public PhoneNumber as String
End Class
Having the same field duplicated in this instance is okay because it is in a collection/array. In this JSON example you have two Customer objects for the Response's Customers collection.
Your object model would look like this:
Public Class Response
Public Property Customers As List(Of Customer)
Public Property Expand As Boolean
Public Property Include As Boolean
End Class
Public Class Customer
Public Property PhoneNumber As String
End Class
An example of deserializing it:
Dim objSerializer As New JavaScriptSerializer()
Dim objResponse As Response = objSerializer.Deserialize(Of Response)(strData)
I've created a request class. Here is an abbreviated version of it:
Public Class Request(Of T)
Private _Account As String
Public Property Account() As String
Get
Return _Account
End Get
Set(ByVal value As String)
_Account = value
End Set
End Property
Private _InnerRequest As T
Public Property InnerRequest() As T
Get
Return Me._InnerRequest
End Get
Set(ByVal value As T)
Me._InnerRequest = value
End Set
End Property
End Class
And then I have two other classes that I intend to use with this one - again, abbreviated
Public Class Individual
Public FirstName As String
Public LastName As String
Friend Sub New()
End Sub
End Class
And
Public Class Commercial
Public EntityName As String
Friend Sub New()
End Sub
End Class
Again, both of these are pretty abbreviated. The issue comes in when I attempt to use the properties of individual or commercial:
Dim Req As New Request(Of Individual)()
Req.InnerRequest.FirstName = "Herman" <-- Null Ref Exception
So... how do I get my inner request null ref exception kicked? I tried simply using Me._InnerRequest = New T in the New sub of Request, but no dice. Is there a way to handle this?
Req.InnerRequest must be set to an object instance of Individual first.
Req.InnerRequest = new Individual()
Req.InnerRequest.FirstName = "Herman"
Or create an instance for InnerRequest with the following modifications
Public Class Request(Of T As {New}) 'Classes of type T must have a public new constructor defined
::
Private _InnerRequest As New T() 'Creates a new class of type T when an instance is created of Request
And make the constructors of the other classes Public instead of Friend.
Than you can directly do
Dim Req As New Request(Of Individual)()
Req.InnerRequest.FirstName = "Herman"
#Barry already answered what the main problem is, but here's an alternate syntax if you prefer object initializers:
Req.InnerRequest = new Individual() With { FirstName = "Herman" }
Or, if you prefer, you could overload the constructor for your Individual class:
Dim individual As New Individual("Herman")
Req.InnerRequest = individual
With the Individual class looking like:
Public Class Individual
Public FirstName As String
Public LastName As String
Friend Sub New()
End Sub
Friend Sub New(firstName As String)
Me.FirstName = firstName
End Sub
End Class
You probably should consider restricting the T to some Entity class:
Public Class Request(Of T As Entity)
From which both Individual and Commercial will inherit:
Public Class Individual : Inherits Entity
Then maybe declare an overridable property Name of type String on this Entity class (which can be abstract/MustInherit), this should provide some flexibility. Otherwise you'd be having a hard time consuming your design pattern.
I cant seem to get my HTML.DropDownListFor() to work properly with my structures.
CategoriesEditViewModel
Public Class CategoriesEditViewModel
Public Property Categories As List(Of cihCategoryOrgDef)
Public Property Category As cihCategoryOrgDef
Public Property lists As cihLists = New cihLists()
Public Property SelectedItem As String
Public Sub New(catId as Guid)
SelectedItem = codId.ToString
lists.loadClubWaivers(ZenCommon.CurrentOrgId)
End Sub
End Class
cihLists
Private orgWaiverList As List(of cihWaiver)
Public Sub loadClubWaivers(orgId as Guid)
'Go to database and populate orgWaiverList
End Sub
Public ReadOnly Property organizationClubWaivers() As List(Of cihWaiver)
Get
Return orgWaiverList
End Get
End Property
cihWaiver
Public Class cihWaiver
Public waiverId As Guid = Guid.Empty
Public waiverName As String = ""
Public waiverText As String = ""
End Class
Edit View Page
#Html.DropDownListFor(Function(m) m.SelectedItem, New SelectList(Model.lists.organizationClubWaivers, "waiverId", "waiverText", Model.lists.organizationClubWaivers))
The error i get is 'Library.cihWaiver' does not contain a property with the name 'waiverId'.
but the cihWaiver class clearly has an item for 'waiverId'. I haven't done MVC stuff in a while, so maybe i'm going about this all wrong.
The answer was as simple as changing my cihWaiver Class to this:
Public Class cihWaiver
Public Property waiverId As Guid = Guid.Empty
Public Property waiverName As String = ""
Public waiverText As String = ""
End Class
cane someone explain to me why the keyword Property is so important?
I am trying to declare type Product and type Productlist
can anyone tell me if this is right way to do it ?
Public Class Product
Public Property name As String
Public Property price As Double
End Class
Public Class ProductsList
Public Property items() As New List(Of Product)
End Class
I mean can I just write
Public Class Product
Public Property name As String
Public Property price As Double
End Class
Public property ProductsList as new List(Of Product)
instead ?
The first approach seems like better practice. You would code with it like this:
Dim p as new Product()
p.name = "Apple"
p.price = 1
Dim pList as new ProductList()
pList.Items.Add(p)
The issue with the second approach is you would have a wrapper class, eg DemoWrapper:
Public Class DemoWrapper
Public Class Product
Public Property name As String
Public Property price As Double
End Class
Public ProductsList as new List(Of Product)
End Class
Your code would end up like this:
Dim p as new DemoWrapper.Product()