J'ai la vue suivante,,, qui crée 10 ajax.beginform ,, Mais le problème que je rencontre est que si une erreur se produit pendant la création de l'objet, alors le ModelState.AddModelError ne sera pas montré sur la vue bien que j'aie défini le paramètre @Html.ValidationSummary(true)
La vue est la suivante
@model Medical.Models.VisitLabResult
@for (int item = 0; item < 10; item++)
{
<tr id = @item>
@using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = item.ToString() + "td",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "progress2",
OnSuccess = string.Format(
"disableform({0})",
Json.Encode(item)),
}))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
<td>
@Html.DropDownList("LabTestID", String.Empty)
@Html.ValidationMessageFor(model => model.LabTestID)
</td>
<td>
@Html.EditorFor(model => model.Result)
@Html.ValidationMessageFor(model => model.Result)
</td>
<td>
@Html.EditorFor(model => model.DateTaken)
@Html.ValidationMessageFor(model => model.DateTaken)
</td>
<td>
@Html.EditorFor(model => model.Comment)
@Html.ValidationMessageFor(model => model.Comment)
</td>
<td>
<input type="submit" value="Create" />
</td>
<td id = @(item.ToString() + "td")>
</td>
}
</tr>
}
</table>
Et ma méthode d'action qui définit le ModelState.AddModelError ressemble à ceci:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateAll(VisitLabResult vlr, int visitid = 28)
{
try
{
if (ModelState.IsValid)
{
var v = repository.GetVisit(visitid);
if (!(v.EligableToStart(User.Identity.Name))){
return View("NotFound");
}
vlr.VisitID = visitid;
repository.AddVisitLabResult(vlr);
repository.Save();
return Content("Addedd Succsfully");
}
}
catch (DbUpdateException)
{
JsonRequestBehavior.AllowGet);
ModelState.AddModelError(string.Empty, "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
}
}
Alors comment puis-je afficher ModelState.AddModelError dans ma vue ?