DropDownListFor using List of Class - vb.net

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?

Related

Filter List of Class by Property Value and List Of Value

I have a Devexpress Gridcontrol bound to a custom class.
The class looks like this:
Public Class AuditList
Public CasualtyList As List(Of CasualtyRecords)
Public MedsList As List(Of CasualtyRecords.Medications)
Public Property FilterString As CriteriaOperator
Public Sub New()
CasualtyList = New List(Of CasualtyRecords)
MedsList = New List(Of CasualtyRecords.Medications)
End Sub
Public Class CasualtyRecords
Private _primary As New PS
Public Property PrimarySurvey As PS
Get
Return _primary
End Get
Set(value As PS)
_primary = value
End Set
End Property
Public Sub New()
Vitals = New List(Of VitalRecords)
End Sub
Public Property Vitals As List(Of VitalRecords)
Public Property Meds As List(Of Medications)
ReadOnly Property MedCount As Integer
Get
Return Meds.Count
End Get
End Property
Property Id As Integer
Property ClinicalImpression As String
Property Disposal As String
Property Age As Integer
Property Gender As String
Class PS
Public Property Airway As Integer
Public Property Breathing As Integer
Public Property Circulation As Integer
Public Property Rate As Integer
End Class
Class Medications
Public Property MedName As String
End Class
End Class
End Class
This is an example of a filter type I am trying to create:
"[Gender] ='Male' AND [Medications].[MedName] = 'Paracetamol' AND [Age] >100"
Is this possible with the class constructed as shown, or perhaps do I need to implement some other interface?
I imagine that it would look something like this with LINQ
Dim b As New CasualtyRecords
b = a.CasualtyList.Where(Function(x) x.Meds.Any(Medications.Med = "Paracetamol") And x.Gender = "Male" And x.Age > 20)
Thanks
I was able to achieve the required results using this LINQ query
Dim newrecords = a.CasualtyList.Where(
Function(x) x.Meds.Any(
Function(b) b.MedName = "Paracetamol") _
And x.Gender = "Male" And x.Age > 20).ToList()

The entity type is not part of the model for the current context - DBFirst

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)

Assigning values to an object and its nested object

I am a novice and am missing something simple. I have two Classes
Public Class Param
Public Property temperature As String
Public Property display As Boolean
Public Property storage As Boolean
Public Property reason As Boolean
Public Property stats As Object
Public Property errors As Object
End Class
Public Class getTemperature
Public Property method As String
Public Property params As Param()
Public Property id As String
End Class
I want to declare and assign values to the objects but I keep getting the error "Object reference not set to an instance of an object" when trying to assign values to items within param. I don't understand, I have created both the object GetTemperature and the object Params, what am I missing?
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
Dim params As New Param
params.temperature = "true"
'GetTemp.params(0) = params
I have also tried, but get the same response:
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
GetTemp.params(0).temperature = "True"
Try this:
Public Class Param
Public Property temperature As String
Public Property display As Boolean
Public Property storage As Boolean
Public Property reason As Boolean
Public Property stats As Object
Public Property errors As Object
End Class
Public Class getTemperature
Public Property method As String
Public Property params As List(Of Param)
Public Property id As String
Public Sub New()
params = New List(Of Param)
End Sub
End Class
So you could write something like:
Dim a As New getTemperature
a.params.Add(New Param)
It will work if you remove the parenthesis
Public Class getTemperature
Public Property method As String
Public Property params As Param
Public Property id As String
End Class
and then
Dim GetTemp As New getTemperature
GetTemp.method = TextBoxMethod.Text
GetTemp.id = TextBoxID.Text
Dim params As New Param
params.temperature = "true"
GetTemp.params = params

Combine two tables into one entity framework? (SQLite)

I am using the EntityFramework.SQLite library and for the life of me can not figure out how to combine two tables together into a temporary table for display purposes in xaml code.
Here's the code for my two tables I want to combine which is about all I am using right now besides a class for my temp table called CategoryList (this is part of the business/data logic dll library):
Partial Public Class CategoryList
Public Sub New()
Me.CategoryInfo = New CategoryReference
'Me.CategoryCode = New HashSet(Of CategoryCodes)
Me.CategoryCodes = New CategoryCodes
End Sub
Public Property MyId As Integer
<Key, ForeignKey("CodeID")>
<Required>
Public Property CodeID As Integer
<Key, ForeignKey("CategoryID")>
Public CategoryID As Integer
'Public Property CategoryCode As ICollection(Of CategoryCodes)
Public Property CategoryInfo As CategoryReference
' Public Property CategoryInfo As ICollection(Of CategoryReference)
Public Property CategoryCodes As CategoryCodes
End Class
<Table("CategoryCodes")>
Public Class CategoryCodes 'category shortnames/codes
<MaxLength(100)>
<Required>
Public Property CategoryCode As String
Get
Return _CategoryCode
End Get
Set(value As String)
_CategoryCode = value
End Set
End Property
Private _CategoryCode As String
'<NotNull>
' <PrimaryKey>
'<Unique(Name:="UQ__CategoryCodes__0000000000000081_CategoryCodes", Order:=0)>
<Key>
<Required>
Public Property CodeID As Integer
Get
Return CategoryCode
End Get
Set(value As Integer)
_CodeID = value
End Set
End Property
Private _CodeID As Integer
End Class
<Table("CategoryReference")>
Partial Public Class CategoryReference 'table design for category data
<MaxLength(100)>
Public Property CategoryName As String
Get
Return _CategoryName
End Get
Set(value As String)
_CategoryName = value
End Set
End Property
Private _CategoryName As String
<MaxLength(100)>
Public Property CategoryDescription As String
Get
Return _CategoryDescription
End Get
Set(value As String)
_CategoryDescription = value
End Set
End Property
Private _CategoryDescription As String
'<Unique(Name:="UQ__CatagoryReference__000000000000005F_CatagoryReference", Order:=0)>
<ForeignKey("CodeID")>
<Required>
Public Property CodeID As Integer
Get
Return _CodeID
End Get
Set(value As Integer)
_CodeID = value
End Set
End Property
Private _CodeID As Integer
'<Unique(Name:="UQ__CatagoryReference__000000000000005A_CatagoryReference", Order:=0)>
<Key>
<Required>
Public Property CategoryID As Integer
Get
Return _CategoryID
End Get
Set(value As Integer)
_CategoryID = value
End Set
End Property
Private _CategoryID As Integer
End Class
It may seem long but the tables are very simple and the get/set blocks make it look long (A vb.net editor can turn them into simple property's if she/he wishes). I may be using the CategoryList class wrong but here's where I use it in my xaml datasource in my main application code (I have business logic/data processing in a dll library):
Private Property ViewModel As List(Of UIELLUWP.DataAccess.CategoryList)
Dim categories As New UIELLUWP.DataAccess.SQLiteDb
ViewModel = categories.Categories.ToList
Errors received with current code:
I receive an error that Table "CategoryList" does not exist when I run the above code.
my solution I found out to this question was to add asnotracking because tracking was taking place.

Need Help Initializing a Generic Property in VB.Net

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.