I have a rather complex model that I am using to render a form and perform validation using the model’s meta information meta information.
The View Model has a list of child objects that are included in the form. the child objects are based on this:
[Table]
public class FieldInstance
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public long fiID { get; set; }
[Column]
public string fiLabel { get; set; }
[Column]
public bool fiIsRequired { get; set; }
[DisplayName("alpha-numeric value")]
[Column]
public string fiStrValue { get; set; }
[DisplayName("date/time value")]
[Column]
public DateTime? fiDateTimeValue { get; set; }
[DisplayName("integer value")]
[Column]
public long? fiIntValue { get; set; }
[DisplayName("decimal value")]
[Column]
public decimal? fiDecValue { get; set; }
[Column]
public int fiOrder { get; set; }
[Column]
public long fiStreamEntryID { get; set; } // FK
[Column]
public long fiFieldTypeID { get; set; } // FK
// Relationship (many FieldInstances to one StreamEntry)
// using EntityRef<StreamEntry> and ThisKey
// which is "This" table's FK
private EntityRef<StreamEntry> _StreamEntry;
[System.Data.Linq.Mapping.Association(Storage = "_StreamEntry", ThisKey = "fiStreamEntryID")]
public StreamEntry StreamEntry
{
get { return this._StreamEntry.Entity; }
set { this._StreamEntry.Entity = value; }
}
// Relationship (one FieldInstance to one FieldType)
// using EntityRef<FieldTypes> and ThisKey
private EntityRef<FieldTypes> _FieldType;
[System.Data.Linq.Mapping.Association(Storage = "_FieldType", ThisKey = "fiFieldTypeID")]
public FieldTypes FieldTypes
{
get { return this._FieldType.Entity; }
set { this._FieldType.Entity = value; }
}
I’m using an Html.EditorFor() statement to render the partial view template for each item in the list.
If I put text into a Datetime field, then Html.ValidationSummary() shows:
•The value 'asd' is not valid for date/time value.
•The value 'asd' is not valid for date/time value.
My problem is that errors are being added to the ModelState twice instead of just once. This is the controller action:
[HttpPost]
public ActionResult EntryEdit(StreamEntry form)
{
// Get values
StreamEntry entry =
form.seID == 0
? new StreamEntry()
: genesisRepository.GetEntryByID(form.seID);
// Get Stream for new entry
if (form.seID == 0)
entry.Stream = genesisRepository.GetStreamByID(form.StreamID);
//Validate
TryUpdateModel(entry);
if (ModelState.IsValid)
return RedirectToAction("EntryList", new { id = entry.StreamID });
else
return View(entry);
}
Why are errors that are triggered by the presence of incorrect input resulting in two instances of the error being added to the `ModelState?
They are added once by the default model binder when it tries to parse the
StreamEntry formaction argument and a second time when you call theTryUpdateModelmethod on the same type (StreamEntry). So either use action arguments or theTryUpdateModelmethod but never both. Personally I always use action parameters and never theTryUpdateModelmethod.So to fix the error in your case: