0

在我的两个部分观点中,我面临验证问题

_partialA.cshtml

@model demo3.Models.ModelA

   @using (Html.BeginForm("TEST", "Home"))    {

<h2>_partialA</h2>
<div>
     @Html.EditorFor(m => m.EmployeeId)  
     @Html.ValidationMessageFor(m => m.EmployeeId) 
</div>
<div>
     @Html.EditorFor(m => m.EmployeeName)
     @Html.ValidationMessageFor(m => m.EmployeeName) 
</div>
 <input class="mainbutton" type="submit" value="TEST"/><br />
 }

另一个部分观点是

_partialB.cs.html

 @model demo3.Models.ModelB
      @using (Html.BeginForm("CreateAgreement", "Home"))
            {

<h2>_partialB</h2>
<div>
    @Html.EditorFor(m => m.Comapny)
    @Html.ValidationMessageFor(m => m.Comapny)
</div>
<div>
    @Html.EditorFor(m => m.FisacalYear)
    @Html.ValidationMessageFor(m => m.FisacalYear)  </div>
  <input class="mainbutton" type="submit" value="CreateAgreement"/><br />
 }

这是我的控制器代码

 [HttpPost]
    public ActionResult CreateAgreement(ModelB modelb)
    {
        if (ModelState.IsValid)
        {
            return View("Start", modelb);
        }
        return View("Start", modelb);
    }
    [HttpPost]
    public ActionResult TEST(ModelA modela)
    {
        if (ModelState.IsValid)
        {
            return View("Start", modela);
        }
        return View("Start", modela);
    }
    public ActionResult Start()
    {
        return View();
    }
    public ActionResult FirstView()
    {
        ModelA objA = new ModelA();
        return PartialView("_partialA", objA);
    }
    public ActionResult SecondView()
    {
        ModelB objB = new ModelB();
        return PartialView("_partialB", objB);
    }

现在我在点击按钮时没有收到任何错误消息..还有另一件事..我的两个视图都被打开了..点击按钮..如何防止这种情况发生?

4

1 回答 1

1

要验证您的表格,请检查以下步骤:

1)在您的局部视图中将布局设置为null

@{
    Layout = null;
}

2)在表单之前添加这些脚本:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

@using (Html.BeginForm("CreateAgreement", "Home"))
 {

3)添加HTML验证:

@using (Html.BeginForm("CreateAgreement", "Home"))
 {
     @Html.ValidationSummary(true)

4)如果您的模型中有注释并且错误消息将起作用。

于 2013-05-03T09:09:30.297 回答