我在我的模型类上定义了以下数据注释:-
[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
会分别调用下面的action方法:-
[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);
}
如上面的代码所示,如果动作执行成功,动作方法将返回“_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”部分视图。如果用户单击“创建”按钮,则不会发生任何事情(视图将继续显示相同的错误和相同的字段!!!)。而用户将能够使用 Internet Explorer 9 重新提交部分视图而不会出现任何问题。
2.第二个问题是除非用户单击“创建”按钮,否则不会显示所有数据注释验证,例如[必需],尽管我在我的应用程序中启用了客户端验证(并且客户端验证正在工作其他观点很好)。所有浏览器(IE9、firefox 和 chrome)都出现此问题。
因此,任何人都可以帮助我找出引发上述两个问题的代码中的问题。* 在此先感谢您的帮助。BR