我在模型类上定义了以下数据注释: -
[Display(Name = "Lab Test Description")]
[Required]
public int LabTestID { get; set; }
[Display(Name = "Test Result")]
[Range(typeof(decimal), "0.00", "9999999999.9999999999")]
[Required]
public decimal Result { get; set; }
然后在视图上定义了以下内容,在表格中显示10个创建表单: -
@model Medical.Models.VisitLabResult
<table>
<tr>
<th>
Lab Test
</th>
<th>
Result
</th>
<th>
Date Taken
</th>
<th>
Comment
</th>
<th>
</th>
</tr>
@for (int item = 0; item < 10; item++)
{
using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = item.ToString(),
InsertionMode = InsertionMode.Replace }))
{
<tr id = "@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.TextBox("DateTaken", null, new { @id = "DateTaken" + item.ToString(), DataFormatString = "{0:D}" , ApplyFormatInEditMode = true })
@Html.ValidationMessageFor(model => model.DateTaken)
</td>
<td>
@Html.EditorFor(model => model.Comment)
@Html.ValidationMessageFor(model => model.Comment)
</td>
<td>
@Html.HiddenFor(model => model.VisitID)
<input type="submit" value="Create" />
</td>
</tr>
}
}
</table>
<div>
@Html.ActionLink("Back To Current Visit", "Edit", "Visit", new { id = Model.VisitID }, null)
</div>
以上10 Beginforms
将分别调用以下操作方法: -
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateAll(VisitLabResult vlr)
{
try
{
if (ModelState.IsValid)
{
repository.AddVisitLabResult(vlr);
repository.Save();
string message = repository.checkrange(vlr.LabTestID,vlr.VisitID);
var lab = repository.GetLabTest(vlr.LabTestID,false);
ViewBag.status = message;
return PartialView("_create",vlr) ;} }
catch (DbUpdateException)
{ ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID);
ModelState.AddModelError("LabTestID", "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
ViewBag.status = "Error";
return PartialView("_createToedit", vlr);
}
ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID);
ViewBag.status = "Error";
return PartialView("_createToedit", vlr);
}
如上面的代码所示,如果动作成功执行,action方法将返回“_create”局部视图。虽然它将返回“_createToedit”局部视图 - 包含任何错误 - 如下所示: -
<td>
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@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>
@Html.HiddenFor(model => model.VisitID)
<input type="submit" value="Create" />
</td>
但目前我面临上述代码的以下两个问题: -
1。当我使用fireFox或chrome浏览器访问系统时,用户将无法重新提交包含ModelState错误和任何数据注释错误的“_createToedit”局部视图。如果用户点击“创建”按钮,则不会发生任何事情(视图将继续显示相同的错误/ s和相同的字段!!!)。虽然用户可以使用Internet Explorer 9重新提交部分视图而没有任何问题。
2. 第二个问题是,除非用户点击“创建”按钮,否则不会显示所有数据注释验证,例如[必需],尽管我已在内部启用了客户端验证我的应用程序(以及客户端验证在其他视图上运行良好)。所有浏览器(IE9,firefox和Chrome)都会出现此问题。
所以任何人都可以帮助我找到我的代码中出现的问题,这会引发上述两个问题。* 在此先感谢您的帮助。 BR