I have a Question about #Html.DropDownListFor
I have 2 class :
Public Class A01WorkRecord
Public Property counter As Int32
Public Property MembetNumber As Int16
Public Property Memo As String
Public Property WorkDate As Date
End Class
Public Class B01Member
Public Property counter As Int32
Public Property Number As Int16
Public Property Name As Date
End Class
the VIEW is for create Work Record
so I want the view can chooise Member Name and write work memo
so in the Controller
Function Index() As ActionResult
Dim q = From p In db.B01s
Select p.Number, p.Name
Dim ListItem As List(Of SelectListItem) = New List(Of SelectListItem)
For Each l In q
ListItem.Add(New SelectListItem With {.Text = l.Name, .Value = l.Number})
Next
ViewData("List") = New SelectList(ListItem, "Value", "Text", "")
Return View()
End Function
and in the View
#Html.DropDownListFor(Function(model) model.MembetNumber ,ViewData("Item"))
I want to choose name, but it's not working ...
the error message:
No Type is 'IEnumerable' and index 'Number' ViewData Item
I don't Know what's Wrong, please help me.
From the example provided it appears that you are might be mixing up ViewData("Item") with ViewData("List")
So try this instead
#Html.DropDownListFor(Function(model) model.MembetNumber, ViewData("List"))
Related
I am trying to get data using GROUP BY statement:
SELECT nama_penerbit
FROM DigitalBook.digitalbook.ebook
ORDER BY nama_penerbit
and store this value.
Using navicat
but did not get the value when I try to get in controller in VB.NET:
<Route("api/Ebooks/GetAllPenerbit")>
<HttpGet>
Function GetAllPenerbit() As Results.JsonResult(Of List(Of ebook))
Dim kategoriEbookRepository As New EbookReposity
Dim d = kategoriEbookRepository.PenerbitEbookRepository()
Return Json(d)
End Function
This is my model class:
Public Function PenerbitEbookRepository() As List(Of ebook)
Dim sql = "Select nama_penerbit From Digitalbook.digitalbook.ebook Group by nama_penerbit"
Dim datas As New List(Of ebook)
Try
datas = db.Database.SqlQuery(Of ebook)(sql).ToList
Catch ex As Exception
End Try
Return datas
End Function
This is my class
<Table("digitalbook.ebook")>
Partial Public Class ebook
<Key>
<StringLength(50)>
Public Property id_ebook As String
<StringLength(500)>
Public Property judul As String
Public Property id_kategori_ebook As Guid?
<StringLength(255)>
Public Property pengarang As String
<StringLength(255)>
Public Property nama_penerbit As String
End Class
i created a new class with the same property, then i used it on controller and model, it works fine
This is my new class
Partial Public Class getebook
Public Property id_ebook As String
Public Property judul As String
Public Property id_kategori_ebook As Guid?
Public Property pengarang As String
Public Property nama_penerbit As String
End Class
My Controller
<Route("api/Ebooks/GetAllPenerbit")>
<HttpGet>
Function GetAllPenerbit() As Results.JsonResult(Of List(Of getebook))
Dim kategoriEbookRepository As New EbookReposity
Dim d = kategoriEbookRepository.PenerbitEbookRepository()
Return Json(d)
End Function
My Model
Public Function PenerbitEbookRepository() As List(Of getebook)
Dim sql = "Select Distinct nama_penerbit From Digitalbook.digitalbook.ebook"
Dim datas As New List(Of getebook)
Try
datas = db.Database.SqlQuery(Of getebook)(sql).ToList
Catch ex As Exception
End Try
Return datas
End Function
I have called clientdetails which I wish to return as a whole to the JSONConvert Method to Serialize for JSON.
I have created a Class that has the Property Types I require (TextA,TextB) etc.
I can refer to both TransactionCount and TransactionType because they are part of ClientDetails, however when I try to refer to TextA of Transactions it states that TextA is not a member of ClientDetails - this I know which is why I explicitly state .Transactions.TextA.
If I declare Transactions separate under a new variable then I am able to refer to them however I need them to all be declared under ClientDetails to pass to the Serializer.
Can anyone point me out what I'm doing wrong here? Still learning.
Public Class JSON
Public Shared Function SerializeObject()
Dim clientdetails As New ClientDetails() With {.TransactionCount = "1", .TransactionType = "Q", .Transactions.TextA} 'Unable to Refer to any Property of Transactions.
'Dim Trans As New Transactions()
'Trans.TextA= "Test"
Dim output As String = JsonConvert.SerializeObject(clientdetails, Newtonsoft.Json.Formatting.Indented)
Return output
End Function
End Class
Public Class ClientDetails
Public Property Transactions As New Transactions()
Public Property [TransactionType] As String
Public Property [TransactionCount] As Integer
End Class
Public Class Transactions
Public Property [RecordID] As String
Public Property No As String
Public Property TextA As String
Public Property TextB As String
Public Property Initial As String
Public Property Flag As String
Public Property Sex As String
Public Property Area As String
Public Property Type As String
Public Property IDNO As String
End Class
You can use this syntax:
Dim clientdetails As New ClientDetails() With {.TransactionCount = "1", .TransactionType = "Q", .Transactions = New Transactions() With {.TextA = "Test"}}
Or a more readable code:
Dim trans As New Transactions
trans.TextA = "Test"
Dim clientDetails As New ClientDetails()
With clientDetails
.TransactionCount = "1"
.TransactionType = "Q"
.Transactions = trans
End With
AutoMapper with VB.NET
I have the following classes below. OrderA With List (Of OrderALineItem) and OrderBList With List (Of OrderB). I want to copy data from OrderA to OrderBList. Which copies ItemName, ItemQty, Price from List (Of OrderALineItem) to List (Of OrderB) and OrderID, CustomerName from OrderA itself. I have found almost all codes in C# and am not able to convert it to vb.net code.
Public Class OrderA
Public Property OrderID As String
Public Property CustomerName As String
Public Property OrderLineItem As List(Of OrderALineItem)
End Class
Public Class OrderALineItem
Public Property ItemName As String
Public Property ItemQty As Integer
Public Property Price As Decimal
End Class
Public Class OrderBList
Public Property OrderBLineItem As List(Of OrderB)
End Class
Public Class OrderB
Public Property OrderID As String
Public Property CustomerName As String
Public Property ItemName As String
Public Property ItemQty As Integer
Public Property Price As Decimal
End Class
My VB.NET code until now is:
Dim mapperConfiguration = New MapperConfiguration(Sub(config)
config.CreateMap(Of OrderALineItem, OrderBList)()
End Sub)
Dim mapper = mapperConfiguration.CreateMapper()
Dim objOrderB = mapper.Map(Of OrderBList)(objOrder.OrderLineItem)
The above code creates and object from copies the data from objOrder.OrderLineItem to OrderBList. That's it.
Can anybody help me out on this in VB.NET.
Note: Am totally new in AutoMapper
Version: AutoMapper 6.2.2.0
Done myself, I hope the code below will be helpful to somebody.
Dim mapperConfiguration = New MapperConfiguration(Sub(config)
config.AddProfile(New CustomProfile_1)
End Sub)
Dim objMapper = mapperConfiguration.CreateMapper()
Dim objOrderB As List(Of Dest_OrderB) = objMapper.Map(Of Src_OrderA, List(Of Dest_OrderB))(objOrderA)
Public Class CustomProfile_1
Inherits Profile
Sub New()
CreateMap(Of Src_OrderALineItem, Dest_OrderB)()
CreateMap(Of Src_OrderA, List(Of Dest_OrderB))() _
.ConstructProjectionUsing(
Function(Src1) Src1.List_Src_OrderALineItem.Select(Function(Src2) New Dest_OrderB _
With {.CustomerName = Src1.CustomerName,
.OrderID = Src1.OrderID,
.ItemName = Src2.ItemName,
.ItemQty = Src2.ItemQty,
.Price = Src2.Price}
).ToList())
End Sub
End Class
Is there a way to get value of a object properties with a propertyinfo object?
psudo code:
propertyinfoObject = Text
myobject.toCommand(propertyinfoObject)
The psudo code above should do the same as
myobject.Text
My goal is to create a simpel Properties form that will work on any object (Later I will use keywords to filter out what options I want the use to see).
My real code
Public Class PropertiesForm
Dim propertyInfoVar() As PropertyInfo
Dim Properties As New Form2
Dim listItem As New ListViewItem
Dim stringarray() As String
Public Sub New(ByRef sender As Object)
propertyInfoVar = sender.GetType().GetProperties()
For Each p In propertyInfoVar
stringarray = {p.Name.ToString, #INSERT VALUE SOMEHOW HERE#}
listItem = New ListViewItem(stringarray)
Properties.ListView1.Items.Add(listItem)
Next
Properties.Visible = True
End Sub
EDIT
Just use propertyGrid as suggested below!
The standard PropertyGrid already does all that for you. Filtering properties is not so obvious, here's how:
The control includes a BrowsableAttributes property which allows you to specify that only properties with the specified attribute value should be shown. You can use existing attributes, or custom ones. This is specifically for tagging visible props:
<AttributeUsage(AttributeTargets.Property)>
Public Class PropertyGridBrowsableAttribute
Inherits Attribute
Public Property Browsable As Boolean
Public Sub New(b As Boolean)
Browsable = b
End Sub
End Class
Apply it to an Employee class to hide pay rates or anything else:
Public Class Employee
<PropertyGridBrowsable(True)>
Public Property FirstName As String
...
<PropertyGridBrowsable(False)>
Public Property PayRate As Decimal
<PropertyGridBrowsable(False)>
Public Property NationalInsuranceNumber As String
Test code:
Dim emp As New Employee With {.Dept = EmpDept.Manager,
.FirstName = "Ziggy",
.PayRate = 568.98D,
...
.NationalInsuranceNumber = "1234567"
}
propGrid.BrowsableAttributes = New AttributeCollection(New PropertyGridBrowsableAttribute(True))
propGrid.SelectedObject = emp
BrowsableAttributes is a collection, so you can add several.
I am running from a problem while iterating over class properties.
I have my first class:
class Item
private _UIN as integer = 0
private _Name as string = ""
private _Category as ItemCategory = new ItemCategory()
public Property UIN() as integer
public property Name() as string
public property Category() as ItemCategory
end class
Now when i iterate over the class properties from following code
Dim AllProps As System.Reflection.PropertyInfo()
Dim PropA As System.Reflection.PropertyInfo
dim ObjA as object
AllProps = new Item().getType().getProperties()
for each propA in AllProps
ObjA = PropA.GetValue(myObj, New Object() {})
debug.write ObjA.GetType().Name
next
I get UIN, Name, ItemCategory but i expected UIN, Name and Category.
I am a bit unclear about this and dun know why this is happening? What should i do to correct it?
I get UIN, Name, ItemCategory
That doesn't make sense. You're retrieving the names of the underlying type of the property, you should've gotten something like System.String, System.Int32 and ItemCategory.
If it's the property names you're after, you can just use PropertyInfo.Name, in your case:
AllProps = new Item().getType().getProperties()
for each propA in AllProps
debug.write propA.Name
next