Bookmark and Share

Avoiding the message 'A value is required.' with ASP.NET MVC 2

Posted on Tue 27 October 2009

This message ('A value is required') is the default validation error message that gets displayed with ASP.NET MVC 2 (preview 2 at the time of writing). The message shows up for each column in the model that fails validation when submitting a form.

The problem I came across was that if you have a column name of 'Id' in a table, when using LINQ to SQL, then the validation automatically checks for a value in this field provided to the model. This is great, apart from the fact that if it is an identity column, and therefore auto generating, then you don't want to provide a value as it gets provided by the database. Fortunately, there is an easy way round this and it is to name columns things like PersonId or ClubId rather than just Id. This causes the framework to not autovalidate this column, and stops the validation message from showing up.


Permalink | Comments (1)
Tags: , , . Categories: ASP.NET | LINQ.

Comments

Comments

1/8/2010 3:27:20 PM #

Hi,

Just use Bind(exclude="Id") before the first parameter of your Create action, it solves the problem ;)

insomniak | Reply

Add a comment

Name, e-mail and your comments are all required fields

biuquote
Loading



< go back to the main blog page

< go up to the top of the page