tags:

views:

199

answers:

2

I created a business object (plain old class with properties, nothing fancy), that has an empty constructor, and I am using it as a strong type in my View for /Member/Create (see code).

This all works fine, I get the object "post back" from the Create View - and my method memberToCreate.Save() actually writes what it is supposed to, to the database.

But after that is done, the create view is displayed again, and the fields that are guid- and datetime-types raise a validation error ("A value is required") - unless they are readonly properties.

I don't want to set the properties to read only, since they are needed as writeable from other places.

How do I get around this? Can I "tag" a property as required/not required?

'
' GET: /Member/Create

Function Create() As ActionResult
    Return View()
End Function

'
' POST: /Member/Create

<AcceptVerbs(HttpVerbs.Post)> _
Function Create(<Bind(Exclude:="MemberId")> ByVal memberToCreate As Biz.Member) As ActionResult
    memberToCreate.Save()
    Return View()
End Function

This get me the validation error:

    Public Property DateOfBirth() As Date
        Get
            Return _DateOfBirth
        End Get
        Set(ByVal value As Date)
            _DateOfBirth = value
        End Set
    End Property

This does not...

    Public Readonly Property DateOfBirth() As Date
        Get
            Return _DateOfBirth
        End Get
    End Property
+2  A: 

Try changing the bind attribute to <Bind(Exclude:="MemberId, DateOfBirth")>.

Or you can make the DateOfBirth property nullable if that's OK for your business logic.

çağdaş
Both of your approaches are good, and taught me something.I need to be able to set the DateOfBirth to either a value or nothing, basically, so excluding it declaratively in the binding is not a viable option. I also tried the IsNullable, which I had not heard of before. Quite interesting concept, but I dont really want to change the properties on the businessmodel to another type, just to work around a validation issue.
Kjensen
Glad the answer was helpful. But it does seem like you want to declare the DateOfBirth property in your class with Nullable(Of Date) type. Which will force the default model binder to see it as an optinal property to bind, so it'll have a null value if the posted form does not contain a value for it.
çağdaş
A: 

After some more research (with the new keywords cagdas supplied me with), I came to this solution:

 '
' POST: /Member/Create

<AcceptVerbs(HttpVerbs.Post)> _
Function Create(<Bind(Exclude:="MemberId,UserId,CreatedBy,ModifiedBy")> ByVal memberToCreate As eForening.Biz.Member) As ActionResult

    If ModelState.Item("DateOfBirth").Value.AttemptedValue = String.Empty Then
        ModelState.Item("DateOfBirth").Errors.Clear()
    End If

    If Not ModelState.IsValid Then
        Return View()
    End If
    memberToCreate.Save()
    Return RedirectToAction("Index")
End Function

This lets me handle both empty and specified values for DateOfBirth - and let's me keep my existing Business Class the way it is (and as it should be, in my oppinion).

I basicly check the supplied value of DateOfBirth, and if the supplied value is an empty string, I will reset the error-count of that field and let validation continue - before checking Modelstate.IsValid.

Maybe there is a more elegant solution to this, but it is the best option I have found so far.

Kjensen