I have created the following view for users to create new items. Ive tried to create a bit of validation so if the user leaves a field blank, then it produces a validation message. However, if the user does leave a field blank, my application crashes on
the following line: "_headline = structuralObject.SetValidValue(value, false) in the Model.Designer.cs file. because: 'This property cannot be set to a null value'.
Part of my Model.Designer.cs file:
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String headline
{
get
{
return _headline;
}
set
{
OnheadlineChanging(value);
ReportPropertyChanging("headline");
_headline = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("headline");
OnheadlineChanged();
}
}
The section of code this applies to is as follows in my Create View:
The following is my AccountModels.cs file where i entered the validation for the View:
[MetadataType(typeof(NewsValidation))]
public partial class News
{
}
public class NewsValidation
{
[Required(ErrorMessage = "Posted date is required")]
public DateTime posted { get; set; }
[Required(ErrorMessage = "Headline is required")]
[Display(Name = "Headline")]
public string headline { get; set; }
[Required(ErrorMessage = "Story body is required")]
public string story { get; set; }
}
I was told it was because my database allowed Null values however since then i have created a new database which no longer allows Nulls. My application still crashes and dont know where to start. Here's the odd thing, when it crashes, i click play for it to continue, and the validation appears. So it looks like the validation works but for some reason by application crashes beforehand.
Hello. This is what you could do to avoid this error. It seems that you do not have the client validation enabled. Probably once you enable it you will see the error message before the form submits.
On the server side, it is a good practice to invoke this method befor saving changes in the controller:
if (ModelState.IsValid)
That instruction will validate the model data and, in case there is no value for any required field, it will return false, so no attempt to save data into the DB will occur.
1cainr
Member
24 Points
62 Posts
Error when testing validation: 'This property cannot be set to a null value.'
Jan 12, 2012 08:24 PM|LINK
Hello,
I have created the following view for users to create new items. Ive tried to create a bit of validation so if the user leaves a field blank, then it produces a validation message. However, if the user does leave a field blank, my application crashes on the following line: "_headline = structuralObject.SetValidValue(value, false) in the Model.Designer.cs file. because: 'This property cannot be set to a null value'.
Part of my Model.Designer.cs file:
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String headline { get { return _headline; } set { OnheadlineChanging(value); ReportPropertyChanging("headline"); _headline = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("headline"); OnheadlineChanged(); } }The section of code this applies to is as follows in my Create View:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>News Details</legend> <br /> Posted Date: <div class="editor-field"> @Html.EditorFor(model => model.posted) @Html.ValidationMessageFor(model => model.posted) </div> <br /> Headline Title: <div class="editor-field"> @Html.EditorFor(model => model.headline) @Html.ValidationMessageFor(model => model.headline) </div> <br />The following is my AccountModels.cs file where i entered the validation for the View:
[MetadataType(typeof(NewsValidation))] public partial class News { } public class NewsValidation { [Required(ErrorMessage = "Posted date is required")] public DateTime posted { get; set; } [Required(ErrorMessage = "Headline is required")] [Display(Name = "Headline")] public string headline { get; set; } [Required(ErrorMessage = "Story body is required")] public string story { get; set; } }I was told it was because my database allowed Null values however since then i have created a new database which no longer allows Nulls. My application still crashes and dont know where to start. Here's the odd thing, when it crashes, i click play for it to continue, and the validation appears. So it looks like the validation works but for some reason by application crashes beforehand.
Can anyone offer any support?
Thank you
MatiasBeccar...
Member
46 Points
8 Posts
Re: Error when testing validation: 'This property cannot be set to a null value.'
Jun 14, 2012 12:03 PM|LINK
Hello. This is what you could do to avoid this error. It seems that you do not have the client validation enabled. Probably once you enable it you will see the error message before the form submits.
On the server side, it is a good practice to invoke this method befor saving changes in the controller:
if (ModelState.IsValid)
That instruction will validate the model data and, in case there is no value for any required field, it will return false, so no attempt to save data into the DB will occur.
Hope it helps!
Matías Beccaría