如何使用相同的模型属性对多个局部视图应用验证

时间:2014-11-24 09:48:43

标签: c# asp.net-mvc-4

我是MVC的新手......开发员工模块项目我需要保存员工教育详细信息(包括SSLC,PUC,毕业),因此我使用相同模型的3个部分视图。

模型是

public  class EmployeeEducationDetailTable
{
  public int EmployeeId { get; set; }
  public int EmployeeEducationDetailId { get; set; }
  [Required(ErrorMessage="Select qualification")]
  public int EmployeeQualificationTypeId { get; set; }
  [Required(ErrorMessage="Enter institute name")]
  public string EmployeeInstituteName { get; set; }
  [Required(ErrorMessage="Enter year of pass")]
  [DataType(DataType.Date)]
  [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
  public DateTime? EmployeeYearOfPass { get; set; }
  [Required(ErrorMessage="Enter percentage")]
  public string EmployeePercentage { get; set; }
  public List<EmployeeEducationDetailTable> employeeEducationList { get; set; }

  public EmployeeEducationDetailTable()
  {
    employeeEducationList = new List<EmployeeEducationDetailTable>();
  }

  public IEnumerable<EmployeeMainTable> employee_MainTable { get; set; }
  public ICollection<EmployeeQualificationTypeTable> employee_QualficationTypeTable { get; set; }
  // public IEnumerable<EmployeeQualificationTypeTable> employee_QualficationTypeTable { get; set; } 
}



 public  class EducationSectionDetails
    {

        public EmployeeEducationDetailTable SSLC { get; set; }
        public EmployeeEducationDetailTable PUC { get; set; }
    }

在我的视图中

@model Employee.DAL.ModelClasses.EducationSectionDetails

@using (Html.BeginForm("CreateEmployeeEducationDetails","Employee",FormMethod.Post))
{
    @Html.ValidationSummary(true)

     <h3>SSLC</h3>
  @Html.Partial("_Emp_Education",Model.SSLC,new ViewDataDictionary() 
  { 
    TemplateInfo = new TemplateInfo() 
      { HtmlFieldPrefix = "SSLC" } })

  <h3>PUC</h3>
  @Html.Partial("_Emp_Education",Model.PUC,new ViewDataDictionary() 
    { TemplateInfo = new TemplateInfo() 
      { HtmlFieldPrefix = "PUC" } })

  <input type="submit" value="Submit" />


}

_Emp_Education部分视图

<table>   
  <tr>
    <td>
      <div class="editor-label">
        @Html.LabelFor(model => model.EmployeeQualificationTypeId,"Qualification")
      </div>
    </td>
    <td>
      <div class="editor-field">
        @Html.DropDownListFor(model => model.EmployeeQualificationTypeId,ViewBag.QualificationTypeId as IEnumerable<SelectListItem>,"--Select--")
        @Html.ValidationMessageFor(model => model.EmployeeQualificationTypeId)
      </div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="editor-label">
        @Html.LabelFor(model => model.EmployeeInstituteName,"University/Institute")
      </div>
    </td>
    <td>
      <div class="editor-field">
        @Html.EditorFor(model => model.EmployeeInstituteName)
        @Html.ValidationMessageFor(model => model.EmployeeInstituteName)
      </div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="editor-label">
        @Html.LabelFor(model => model.EmployeeYearOfPass,"Year Of Pass")
      </div>
    </td>
    <td>
      <div class="editor-field">
        @Html.EditorFor(model => model.EmployeeYearOfPass)
        @Html.ValidationMessageFor(model => model.EmployeeYearOfPass)
      </div>
    </td>
  </tr>
  <tr>
    <td>
      <div class="editor-label">
        @Html.LabelFor(model => model.EmployeePercentage,"Percentage/Grade")
      </div>
    </td>
    <td>
      <div class="editor-field">
        @Html.EditorFor(model => model.EmployeePercentage)
        @Html.ValidationMessageFor(model => model.EmployeePercentage)
      </div>
    </td>
  </tr>
</table>

在控制器

 public ActionResult CreateEmployeeEducationDetails()
        {
            EducationSectionDetails e = new EducationSectionDetails();
            e.SSLC = new EmployeeEducationDetailTable();
            e.PUC = new EmployeeEducationDetailTable();
               return View(e);

        }




 [HttpPost]
        public ActionResult CreateEmployeeEducationDetails(EducationSectionDetails tbl)
        {
          //Code 
         }

它是成功发布SSLC,PUC的数据,但问题是验证需要在一个提交按钮上执行分段。有时部分是可选的用户可能会也可能不会输入特定部分的数据。在这种情况下如何处理验证

0 个答案:

没有答案