I’ve just created a new controller, along with its CRUD forms, etc, with a database-first EF model/entity.
Its throwing a number of validation errors on save, but since the form has validators, I don’t see why this would be so.
For reasons that are beyond me, I’m not getting any validation to happen at all. It just goes right in to the saveChanges() call, which promptly fails.
Here’s the edit form:
@model StatementsApplication.DAL.StatementTask
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>StatementTask</legend>
<div class="editor-label">
@Html.LabelFor(model => model.sInitials)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sInitials)
@Html.ValidationMessageFor(model => model.sInitials)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.dtCompleted)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.dtCompleted)
@Html.ValidationMessageFor(model => model.dtCompleted)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.sGroupLabel)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sGroupLabel)
@Html.ValidationMessageFor(model => model.sGroupLabel)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.nGroupSequence)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.nGroupSequence)
@Html.ValidationMessageFor(model => model.nGroupSequence)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.sTaskType)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sTaskType)
@Html.ValidationMessageFor(model => model.sTaskType)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.sTaskLabel)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sTaskLabel)
@Html.ValidationMessageFor(model => model.sTaskLabel)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.nTaskSequence)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.nTaskSequence)
@Html.ValidationMessageFor(model => model.nTaskSequence)
</div>
@Html.HiddenFor(model => model.ID)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
and here’s the generated model:
namespace StatementsApplication.DAL
{
using System;
using System.Collections.Generic;
public partial class StatementTask
{
public int StmtBatchID { get; set; }
public string sInitials { get; set; }
public Nullable<System.DateTime> dtCompleted { get; set; }
public string sGroupLabel { get; set; }
public double nGroupSequence { get; set; }
public string sTaskType { get; set; }
public string sTaskLabel { get; set; }
public double nTaskSequence { get; set; }
public int ID { get; set; }
public virtual StatementBatch tblStmtBatch { get; set; }
}
}
and here’s the controller bits…
//
// GET: /StatementTask/Edit/5
public ActionResult Edit(int id = 0)
{
StatementTask statementtask = db.StatementTasks.Find(id);
if (statementtask == null)
{
return HttpNotFound();
}
ViewBag.StmtBatchID = new SelectList(db.StatementBatches, "ID", "sStatus", statementtask.StmtBatchID);
return View(statementtask);
}
//
// POST: /StatementTask/Edit/5
[HttpPost]
public ActionResult Edit(StatementTask statementtask)
{
if (ModelState.IsValid)
{
try
{
db.Entry(statementtask).State = EntityState.Modified;
db.SaveChanges();
}
catch (Exception ex) {
throw ex;
}
return RedirectToAction("Index");
}
ViewBag.StmtBatchID = new SelectList(db.StatementBatches, "ID", "sStatus", statementtask.StmtBatchID);
return View(statementtask);
}
Its a matter of some confusion for me as to why sInitials is throwing ‘required’ validation errors, as well as why sGroupLabel is throwing length validation errors.
Thanks
a) your model has no data validation annotations. As such, MVC doesn’t do any validation, because you’re not telling it what to validate.
b) You don’t mention what you are submitting. Are you just submitting an empty form?