C# 如何使用相同的模型特性对多个局部视图应用验证
我是MVC新手。我正在开发一个员工模块项目,我需要保存员工教育详细信息,包括SSLC、PUC、毕业。因此,我使用3个部分视图,使用相同的模型 模型是C# 如何使用相同的模型特性对多个局部视图应用验证,c#,asp.net-mvc-4,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 qualif
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; }
}
它正在成功地发布SSLC、PUC的数据,但问题是验证需要通过一个提交按钮按节执行。有时节是可选的,用户可以输入或不输入相应节的数据。在这种情况下,如何处理验证?没有得到验证是您的最小问题。这将创建重复的id无效的html和name属性,这样它就不会发回并绑定到您的模型Hello Stephen您可以再次查看代码了吗..您应该创建视图模型来表示要显示和编辑的内容。您的EmployeeEducationDetailTable模型确实有意义,为什么它包含EmployeeEducationDetailTable列表?你说过你想要3个教育细节,但你只渲染了2个部分,这是什么?。对于EmployeeEducationDetailTable视图模型,您还应该使用自定义的EditorTemplate,而不是分部,然后@Html.EditorForm=>m.SSLC。至于验证问题,您要么有要么没有。对于可选的详细信息,您需要有一个不带验证属性的重复视图模型,或者您需要使用jquery来禁用对要忽略的控件的验证,但是什么会触发这一点呢?
@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" />
}
<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
}