73162

ASP.NET MVC 2.0 Unused Model Property being called when posting a product to the server?

Question:

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)

i can get rid of the problem, by just wrapping the commented out if statement in the code, but this shouldnt be called to begin with, and that's the issue im having.

Answer1:

It looks like you are experiencing this problem (may be same issue?):

<a href="https://stackoverflow.com/questions/2211829/why-does-asp-net-mvc-care-about-my-read-only-properties-during-databinding" rel="nofollow">Why does ASP.NET MVC care about my read only properties during databinding?</a>

Recommend

  • Set User Control's default event
  • jQuery ajax security
  • Xamarin iOS debugger not hitting breakpoints
  • R DataTable Join and constrain rows
  • PHP file_exists() anomaly
  • Azure table query partial partitionkey guid match
  • Entity Framework ObjectContext: Concurrency
  • CSS how to fix an element to scroll horizontally with the page but not vertically?
  • Accessing the variables from a PHP Anonymous Function
  • Java : How to tint this PNG programmatically?
  • Losing my session variables
  • Why can't UI components be accessed from a backgroundworker?
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Can my PDF ping my server when it is opened?
  • HttpListener.IsSupported is false on XP SP3
  • How to retrieve information from antrun back to maven?
  • How to make R's read_csv2() recognise the text characters properly
  • SonarQube: Cannot deactivate rule with missing quality profile
  • Implementing “partial void” in VB
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • jQuery ready not fired after rails link_to is clicked
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • CSS Linear-gradient formatting issue accross different browsers
  • How would I use PHP exceptions to define a redirect?
  • Trying to switch camera back to front but getting exception
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?