ASP.NET MVC 2.0 Unused Model Property being called when posting a product to the server?
- by Erx_VB.NExT.Coder
i have my auto-generated linq to sql classes, and i extend this class using partial classing (instead of using inheritance), and i have properties that that i've put in later which are not part of the database model and should not be. these are things like "FinalPrice" and "DisplayFinalPrice" - in the dbase, there is only RetailPrice and WholesalePrice so FinalPrice etc are more like extensions of the dbase fields.
when i submit the form with nothing filled in, "FinalPrice" gets called (the 'get' of the property) even tho i never ask for it to be, and even tho it is not needed. this happens before validation, so i don't even get the validation errors i would get.
i've tried using and on the FinalPrice and FinalPriceDisplay properties - no go! why does this happen and how can i stop it from happening? is the modelstate just trying to validate everything so therefore it calls every item no matter what?
for those interested, here is all the code...
Partial Public Class tProduct
'Inherits tProduct
Private Const CommissionMultiplier As Decimal = CDec(1.18)
Private _FinalPrice As Decimal?
Private _DisplayFinalPrice As String
Private _DisplayNormalPrice As String
Public Property CategoryComplete As Short
<ScaffoldColumn(False)>
Public ReadOnly Property FinalPrice As Decimal
Get
'If RetailPrice IsNot Nothing OrElse WholesalePrice IsNot Nothing Then
If _FinalPrice Is Nothing Then
If RetailPrice IsNot Nothing Then
_FinalPrice = RetailPrice
Else
_FinalPrice = WholesalePrice * CommissionMultiplier ' TODO: this should be rounded to the nearest 5th cent so prices don't look weird.
End If
Dim NormalPart = Decimal.Floor(_FinalPrice.Value)
Dim DecimalPart = _FinalPrice.Value - NormalPart
If DecimalPart = 0 OrElse DecimalPart = 0.5 Then
Return _FinalPrice
ElseIf DecimalPart > 0 AndAlso DecimalPart < 0.5 Then
DecimalPart = 0.5 ' always rounded up to the nearest 50 cents.
ElseIf DecimalPart > 0.5 AndAlso DecimalPart < 1 Then
' Only in this case round down if its about to be rounded up to a valeu like 20, 30 or 50 etc as we want most prices to end in 9.
If NormalPart.ToString.LastChr.ToInt = 9 Then
DecimalPart = 0.5
Else
DecimalPart = 1
End If
End If
_FinalPrice = NormalPart + DecimalPart
End If
Return _FinalPrice
'End If
End Get
End Property
<ScaffoldColumn(False)>
Public ReadOnly Property DisplayFinalPrice As String
Get
If _DisplayFinalPrice.IsNullOrEmpty Then
_DisplayFinalPrice = FormatCurrency(FinalPrice, 2, TriState.True)
End If
Return _DisplayFinalPrice
End Get
End Property
Public ReadOnly Property DisplayNormalPrice As String
Get
If _DisplayNormalPrice.IsNullOrEmpty Then
_DisplayNormalPrice = FormatCurrency(NormalPrice, 2, TriState.True)
End If
Return _DisplayNormalPrice
End Get
End Property
Public ReadOnly Property DivID As String
Get
Return "pdiv" & ProductID
End Get
End Property
End Class
more...
i get busted here, with a null reference exception telling me it should contain a value...
Dim NormalPart = Decimal.Floor(_FinalPrice.Value)