user2165424
user2165424

Reputation:

mvc model with foreign key relationship

I am having data annotation validation issue with model containing model of foreign object.

Lets say

Class Foo
{
    public virtual Int Id {get; set;}

    [Required]       
    public virtual String Name {get; set;}
 }

 Class Bar
 {
    public virtual Int Id {get; set;}

    [Required]
    public virtual String AnotherName {get; set;}

    public virtual Foo foo  {get; set;}
 }

FOO is NOT mandatory/required while creating BAR.

But when i try to check the ModelState in HTTPPOST of BAR it say model state is invalid, and report the column of FOO as missing.

[HTTPPOST]
public ActionResult SaveBar(BAR bar)
{
if (ModelState.IsValid)
    SaveBar(bar);

} 

I have set FOO as null, as tried TryUpdateModel, but still didnt help.

EDIT I am dealing with entities and not view models

And also.. How to tell model binder not to check the FOO when binding BAR...

EDIT modified example..

Upvotes: 0

Views: 1617

Answers (4)

Flavia
Flavia

Reputation: 561

@hgrathi, what you are not understanding is that as soon as you made Name in Foo Required, Foo is now Required under Bar.

One way to solve this is to create a custom model binder by implementing IModelBinder.

Upvotes: 0

GHP
GHP

Reputation: 3231

It appears that the model binder is instantiating Foo as a member of the Bar object when you hit 'SaveBar()'. Thus, the attribute validation on Foo fires, just as you would expect.

Here's what I would do: remove the attribute validation and just go a different route, maybe like a custom validation method that you call from the controller actions for normal "Foo" operations. "SaveBar()" can check Foo for a default state and decide to call that validation or not, depending.

Upvotes: 2

Chris Pratt
Chris Pratt

Reputation: 239290

Don't use [Required] on your model's id. I'm not sure whether you're dealing with entities or view models here, but in general, the id should be allowed to be unset. In the case of entities, the id will not be set until it is saved to the database (typically) and in the case of a view model, you might be representing an entity that has not previously been saved (and thus has no id).

[Required] has only two real uses, so you should understand exactly what those are and then only apply the attribute accordingly:

1) On an entity, [Required] will imply NOT NULL. It really only makes sense on strings since every other scalar type is NOT NULL by default.

2) On a view model being used to accept form data from a POST. This will require that the field not be blank. If your id is not a form field being presented to the user for input (which I highly doubt it is), then it should not be required.

Upvotes: 0

von v.
von v.

Reputation: 17108

It doesn't work the way you think it is. The ModelState is invalid because you declared the fields of Foo as required. If your view does not accept and pass values for Foo.Id and Foo.Name then you will get an invalid ModelState.

Upvotes: 0

Related Questions