Question

I'm trying to build a "New Customer" form, so I'm using the default model binder to build my Customer object:

public ActionResult New(Customer customer) { ... }

The only problem is I have a few properties on the Customer object that aren't required. When I leave these fields blank, ASP.NET MVC automatically throws up model errors stating "A value is required".

How can I signal to the framework that these properties are optional?

Was it helpful?

Solution

I'm not 100% but making them nullable should work.

OTHER TIPS

I have the same problem, and I found the solution for primitive types, and that is by making the variable nullable, such as

public virtual int? AnotherId {get; set;}
public virtual string? Name {get; set;}

However, for properties that are objects, I couldn't figure it out. I still have a problem with it.

public virtual Sponsor Sponsor { get; set; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top