我有一个包含多种形式的页面。 表单是在具有多个模型的局部视图概念中实现的。 所有这些模型都在一个剃须刀页面中调用。现在我面临的是,当我提交我的 ModelValidationState.Valid 的第一个表单时,总是返回false。 当检查其有效性时,还将验证其他形式的输入字段。 如何仅验证一个模型?
@ Karney
我正在使用Razor Pages。没有为此的控制器
在此处粘贴您要求的代码及其文件夹结构
页面/员工/入职/Index.cshtml
@page
@model Contractor_HRMS.Pages.Staff.Onboarding.IndexModel
@{
ViewData["Title"] = "Staff Onboarding";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h4>STAFF ONBOARDING</h4>
<hr />
<div id="wizard">
<!-- Tabstrip -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="nav-item active">
<a href="#StaffDetails" role="tab" class="nav-link" data-toggle="tab">Staff Details</a>
</li>
<li role="presentation" class="nav-item">
<a href="#Biodata" class="nav-link" role="tab" data-toggle="tab">Biodata</a>
</li>
<li role="presentation" class="nav-item">
<a href="#EduQualification" class="nav-link" role="tab" data-toggle="tab">Educational Qualification</a>
</li>
<li role="presentation" class="nav-item">
<a href="#Assets" class="nav-link" role="tab" data-toggle="tab">Assets</a>
</li>
<li role="presentation" class="nav-item">
<a href="#Others" class="nav-link" role="tab" data-toggle="tab">Others</a>
</li>
<li role="presentation" class="nav-item">
<a href="#AttachForms" class="nav-link" role="tab" data-toggle="tab">Attachment Of Forms</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<!-- STAFF DETAILS -->
<div role="tabpanel" class="tab-pane active" id="StaffDetails">
<br />
<form method="post" asp-page-handler="InsertStaffDetails">
<partial name="_StaffDetails" />
<div style="float:right">
<input type='submit' class='btn btn-success' value='Save' />
</div>
</form>
</div>
<!-- BIODATA -->
<div role="tabpanel" class="tab-pane" id="Biodata">
<br />
<form method="post" asp-page-handler="InsertBioData">
<partial name="_BioData" />
<div style="float:right">
<input type='submit' class='btn btn-success' value='Save' />
</div>
</form>
</div>
<!-- EDUCATIONAL QUALIFICATION -->
<div role="tabpanel" class="tab-pane" id="EduQualification">
<br />
<form method="post" asp-page-handler="InsertEduDetails">
<partial name="_EduQualification" />
<div style="float:right">
<input type='submit' class='btn btn-success' value='Save' />
</div>
</form>
</div>
<!-- ASSETS -->
<div role="tabpanel" class="tab-pane" id="Assets">
<br />
<partial name="_StaffAssets" />
</div>
<!-- OTHERS -->
<div role="tabpanel" class="tab-pane" id="Others">
<br />
<form method="post">
<partial name="_Others" />
<div style="float:right;">
<input type='button' class='btn btn-success' value='Save' />
</div>
</form>
</div>
<!-- ATTACHMENT OF FORMS -->
<div role="tabpanel" class="tab-pane" id="AttachForms">
<br />
<form method="post">
<partial name="_AttachmentOfForms" />
<div style="float:right;">
<input type='button' class='btn btn-success' value='Save' />
</div>
</form>
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
页面/员工/入职/Index.cshtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Contractor_HRMS.Data;
using Contractor_HRMS.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Contractor_HRMS.Pages.Staff.Onboarding
{
public class IndexModel : PageModel
{
private readonly ApplicationDbContext _context;
public IndexModel(ApplicationDbContext applicationDbContext)
{
_context = applicationDbContext;
}
[BindProperty]
public StaffDetails StaffDetails { get; set; }
[BindProperty]
public StaffBioData StaffBioData { get; set; }
[BindProperty]
public StaffEduDetails StaffEduDetails { get; set; }
[BindProperty]
public StaffAssets StaffAssets { get; set; }
[BindProperty]
public StaffOthers StaffOthers { get; set; }
//---Type Of Employee ---//
public SelectList ShowTypeOfEmployee { get; set; }
//---Employment Type ----//
public SelectList ShowEmploymentType { get; set; }
//---Location ----------//
public SelectList ContractorLocation { get; set; }
//--- Department -------//
public SelectList DeptName { get; set; }
//-------Employment Category --------//
public SelectList EmpCategories { get; set; }
//---------Employment Status ---------//
public SelectList EmpStatus { get; set; }
//------------Regular/Temporary ----------//
public SelectList RegularOrTemporary { get; set; }
// ------------Incentive Plan ----------------//
public SelectList IncentivePlans { get; set; }
//-----------Gender--------------------------//
public SelectList StaffGender { get; set; }
//--------------Religion---------------------//
public SelectList StaffReligion { get; set; }
//--------------WorkPass Type------------------//
public SelectList WorkpassTypeList { get; set; }
//-----------Highest Qualification -------------------//
public SelectList Qualification { get; set; }
//-----------Asset Name -------------------//
public SelectList ShowAssetName { get; set; }
//----------------Othes--------------------------//
public string[] status = new[] { "Yes", "No" };
public void OnGet()
{
ShowTypeOfEmployee = new SelectList(_context.TypeOfEmployees, "TypeOfEmployee", "TypeOfEmployee");
ShowEmploymentType = new SelectList(_context.EmploymentTypes, "EmploymentType", "EmploymentType");
ContractorLocation = new SelectList(_context.Location.OrderBy(e => e.location), "location", "location");
DeptName = new SelectList(_context.Department.OrderBy(e => e.DeptName), "DeptName", "DeptName");
EmpCategories = new SelectList(_context.EmploymentCategory.OrderBy(e => e.EmpCategories), "EmpCategories", "EmpCategories");
EmpStatus = new SelectList(_context.EmploymentStatus.OrderBy(e => e.EmpStatus), "EmpStatus", "EmpStatus");
RegularOrTemporary = new SelectList(_context.RegTemp.OrderBy(e => e.RegularOrTemporary), "RegularOrTemporary", "RegularOrTemporary");
IncentivePlans = new SelectList(_context.IncentivePlan, "IncentivePlans", "IncentivePlans");
StaffGender = new SelectList(_context.Gender, "StaffGender", "StaffGender");
StaffReligion = new SelectList(_context.Religion, "StaffReligion", "StaffReligion");
WorkpassTypeList = new SelectList(_context.WorkpassType.OrderBy(e=>e.workpassType), "workpassType", "workpassType");
Qualification = new SelectList(_context.HighestQual, "Qualfication", "Qualfication");
ShowAssetName = new SelectList(_context.Assets, "assetname", "assetname");
}
public async Task<IActionResult> OnPostInsertStaffDetailsAsync()
{
ShowTypeOfEmployee = new SelectList(_context.TypeOfEmployees, "TypeOfEmployee", "TypeOfEmployee");
ShowEmploymentType = new SelectList(_context.EmploymentTypes, "EmploymentType", "EmploymentType");
DHLLocation = new SelectList(_context.Location.OrderBy(e => e.location), "location", "location");
DeptName = new SelectList(_context.Department.OrderBy(e => e.DeptName), "DeptName", "DeptName");
EmpCategories = new SelectList(_context.EmploymentCategory.OrderBy(e => e.EmpCategories), "EmpCategories", "EmpCategories");
EmpStatus = new SelectList(_context.EmploymentStatus.OrderBy(e => e.EmpStatus), "EmpStatus", "EmpStatus");
RegularOrTemporary = new SelectList(_context.RegTemp.OrderBy(e => e.RegularOrTemporary), "RegularOrTemporary", "RegularOrTemporary");
IncentivePlans = new SelectList(_context.IncentivePlan, "IncentivePlans", "IncentivePlans");
if (ModelState.GetFieldValidationState("StaffDetails") == ModelValidationState.Valid)
{
await _context.StaffDetails.AddAsync(StaffDetails);
await _context.SaveChangesAsync();
return Page();
}
else
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
return Page();
}
}
public async Task<IActionResult> OnPostInsertBioDataAsync()
{
StaffGender = new SelectList(_context.Gender, "StaffGender", "StaffGender");
StaffReligion = new SelectList(_context.Religion, "StaffReligion", "StaffReligion");
WorkpassTypeList = new SelectList(_context.WorkpassType.OrderBy(e => e.workpassType), "workpassType", "workpassType");
if (ModelState.GetFieldValidationState("StaffBioData") == ModelValidationState.Valid)
{
await _context.StaffBioData.AddAsync(StaffBioData);
await _context.SaveChangesAsync();
return Page();
}
else
{
return Page();
}
}
public async Task<IActionResult> OnPostInsertEduDetailsAsync()
{
Qualification = new SelectList(_context.HighestQual, "Qualfication", "Qualfication");
if (ModelState.GetFieldValidationState("StaffEduDetails") == ModelValidationState.Valid)
{
await _context.StaffEduDetails.AddAsync(StaffEduDetails);
await _context.SaveChangesAsync();
return Page();
}
else
{
return Page();
}
}
public async Task<IActionResult> OnPostInsertAssetsDetailsAsync()
{
ShowAssetName = new SelectList(_context.Assets, "assetname", "assetname");
if (ModelState.GetFieldValidationState("StaffAssets") == ModelValidationState.Valid)
{
await _context.StaffAssets.AddAsync(StaffAssets);
await _context.SaveChangesAsync();
return Page();
}
else
{
return Page();
}
}
}
}
StaffDetails的局部视图 Pages / Staff / Onboarding / _StaffDetails.cshtml
@model Contractor_HRMS.Pages.Staff.Onboarding.IndexModel
@*
For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
<div class="row">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="col-6">
<!-- Type Of Employee -->
<div class="form-group">
<label asp-for="StaffDetails.TypeOfEmployee" class="control-label-staff"></label>
<select asp-for="StaffDetails.TypeOfEmployee" asp-items="Model.ShowTypeOfEmployee" class="form-control-staff">
<option value="">Please Select</option>
</select><br/>
<span asp-validation-for="StaffDetails.TypeOfEmployee" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Employment Type -->
<div class="form-group">
<label asp-for="StaffDetails.EmploymentType" class="control-label-staff"></label>
<select asp-for="StaffDetails.EmploymentType" asp-items="Model.ShowEmploymentType" class="form-control-staff">
<option value="">Please Select</option>
</select><br/>
<span asp-validation-for="StaffDetails.EmploymentType" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Employee Id -->
<div class="form-group">
<label asp-for="StaffDetails.EmpID" class="control-label-staff"></label>
<input asp-for="StaffDetails.EmpID" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.EmpID" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Global Id -->
<div class="form-group">
<label asp-for="StaffDetails.GlobalId" class="control-label-staff"></label>
<input asp-for="StaffDetails.GlobalId" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.GlobalId" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Employee First Name(Preferred) -->
<div class="form-group">
<label asp-for="StaffDetails.PreferredEmpFname" class="control-label-staff"></label>
<input asp-for="StaffDetails.PreferredEmpFname" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.PreferredEmpFname" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Employee Last Name(Preferred) -->
<div class="form-group">
<label asp-for="StaffDetails.PreferredEmpLname" class="control-label-staff"></label>
<input asp-for="StaffDetails.PreferredEmpLname" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.PreferredEmpLname" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Location -->
<div class="form-group">
<label asp-for="StaffDetails.Location" class="control-label-staff"></label>
<select asp-for="StaffDetails.Location" asp-items="Model.ContractorLocation" class="form-control-staff">
<option value="">Please Select</option>
</select><br />
<span asp-validation-for="StaffDetails.Location" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Department -->
<div class="form-group">
<label asp-for="StaffDetails.Department" class="control-label-staff"></label>
<select asp-for="StaffDetails.Department" asp-items="Model.DeptName" class="form-control-staff">
<option value="">Please Select</option>
</select><br />
<span asp-validation-for="StaffDetails.Department" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Section -->
<div class="form-group">
<label asp-for="StaffDetails.Section" class="control-label-staff"></label>
<input asp-for="StaffDetails.Section" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.Section" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Job-Title -->
<div class="form-group">
<label asp-for="StaffDetails.JobTitle" class="control-label-staff"></label>
<input asp-for="StaffDetails.JobTitle" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.JobTitle" class="text-danger" style="margin-left:210px;"></span>
</div>
</div>
<div class="col-6">
<!-- Cost-Center -->
<div class="form-group">
<label asp-for="StaffDetails.CostCentre" class="control-label-staff"></label>
<input asp-for="StaffDetails.CostCentre" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.CostCentre" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Employment-Category -->
<div class="form-group">
<label asp-for="StaffDetails.EmploymentCategory" class="control-label-staff"></label>
<select asp-for="StaffDetails.EmploymentCategory" asp-items="Model.EmpCategories" class="form-control-staff">
<option value="">Please Select</option>
</select><br />
<span asp-validation-for="StaffDetails.EmploymentCategory" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Contractor/Agency Name -->
<div class="form-group">
<label asp-for="StaffDetails.ContractorAgencyName" class="control-label-staff"></label>
<input asp-for="StaffDetails.ContractorAgencyName" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.ContractorAgencyName" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Start Date -->
<div class="form-group">
<label asp-for="StaffDetails.Startdate" class="control-label-staff"></label>
<input asp-format="{0:dd/MM/yyyy}" asp-for="StaffDetails.Startdate" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.Startdate" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Supervisor Name -->
<div class="form-group">
<label asp-for="StaffDetails.SupervisorName" class="control-label-staff"></label>
<input asp-for="StaffDetails.SupervisorName" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.SupervisorName" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Compensation -->
<div class="form-group">
<label asp-for="StaffDetails.Compensation" class="control-label-staff"></label>
<input asp-for="StaffDetails.Compensation" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.Compensation" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Employment Status -->
<div class="form-group">
<label asp-for="StaffDetails.EmploymentStatus" class="control-label-staff"></label>
<select asp-for="StaffDetails.EmploymentStatus" asp-items="Model.EmpStatus" class="form-control-staff">
<option value="">Please Select</option>
</select><br />
<span asp-validation-for="StaffDetails.EmploymentStatus" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Regular/Temporary -->
<div class="form-group">
<label asp-for="StaffDetails.RegularTemp" class="control-label-staff"></label>
<select asp-for="StaffDetails.RegularTemp" asp-items="Model.RegularOrTemporary" class="form-control-staff">
<option value="">Please Select</option>
</select><br />
<span asp-validation-for="StaffDetails.RegularTemp" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Incentive Plan -->
<div class="form-group">
<label asp-for="StaffDetails.IncentivePlan" class="control-label-staff"></label>
<select asp-for="StaffDetails.IncentivePlan" asp-items="Model.IncentivePlans" class="form-control-staff">
<option value="">Please Select</option>
</select><br />
<span asp-validation-for="StaffDetails.IncentivePlan" class="text-danger" style="margin-left:210px;"></span>
</div>
<!-- Target Incentive -->
<div class="form-group">
<label asp-for="StaffDetails.TargetIncentive" class="control-label-staff"></label>
<input asp-for="StaffDetails.TargetIncentive" class="form-control-staff" /><br />
<span asp-validation-for="StaffDetails.IncentivePlan" class="text-danger" style="margin-left:210px;"></span>
</div>
<div class="form-group" style="display:none;">
<label asp-for="StaffDetails.LastModifiedBy" class="control-label"></label>
<input asp-for="StaffDetails.LastModifiedBy" class="form-control" value="jteena" />
<span asp-validation-for="StaffDetails.LastModifiedBy" class="text-danger"></span>
</div>
<div class="form-group" style="display:none;">
<label asp-for="StaffDetails.LastModifiedTimestamp" class="control-label"></label>
<input asp-for="StaffDetails.LastModifiedTimestamp" class="form-control" value="@DateTime.Now"/>
<span asp-validation-for="StaffDetails.LastModifiedTimestamp" class="text-danger"></span>
</div>
</div>
</div>
Models / StaffDetails.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Contractor_HRMS.Models
{
public class StaffDetails
{
//--- Staff Id ---//
[Key]
public int StaffID { get; set; }
//--- Emp Id ---//
[Required(ErrorMessage = "Please enter Employee ID")]
[Display(Name = "Employee ID*"),StringLength(100)]
//[PageRemote(PageHandler = "IsEmployeeIDUse", HttpMethod = "get")]
public string EmpID { get; set; }
//--- Global Id ---//
[Display(Name = "Global ID"), StringLength(50)]
public string GlobalId { get; set; }
//--Type of Employee ---//
[Required(ErrorMessage = "Please select Type Of Employee")]
[Display(Name = "Type Of Employee*"),StringLength(100)]
public string TypeOfEmployee { get; set; }
//--Employment Type ---//
[Required(ErrorMessage = "Please select Employment Type")]
[Display(Name = "Employment Type*"), StringLength(100)]
public string EmploymentType { get; set; }
//--Employment Category ---//
[Required(ErrorMessage = "Please select Employment Category")]
[Display(Name = "Employment Category*"), StringLength(50)]
public string EmploymentCategory { get; set; }
//--Employment Status ---//
[Required(ErrorMessage = "Please select Employment Status")]
[Display(Name = "Employment Status*"), StringLength(50)]
public string EmploymentStatus { get; set; }
//--Regular/Temporary ---//
[Required(ErrorMessage = "Please select Regular/Temporary")]
[Display(Name = "Regular/Temporary*"), StringLength(50)]
public string RegularTemp { get; set; }
//--Preferred Employee First Name ---//
[Required(ErrorMessage = "Please enter Employee First Name(Preferred)")]
[Display(Name = "Employee First Name(Preferred)*"), StringLength(100)]
public string PreferredEmpFname { get; set; }
//--Preferred Employee Last Name ---//
[Required(ErrorMessage = "Please enter Employee Last Name(Preferred)")]
[Display(Name = "Employee Last Name(Preferred)*"), StringLength(100)]
public string PreferredEmpLname { get; set; }
//--Location ---//
[Required(ErrorMessage = "Please select Location")]
[Display(Name = "Location*"), StringLength(100)]
public string Location { get; set; }
//--Department ---//
[Required(ErrorMessage = "Please enter Department")]
[Display(Name = "Department*"), StringLength(100)]
public string Department { get; set; }
//--Section ---//
[Required(ErrorMessage = "Please enter Section")]
[Display(Name = "Section*"), StringLength(100)]
public string Section { get; set; }
//--JobTitle ---//
[Required(ErrorMessage = "Please enter JobTitle")]
[Display(Name = "JobTitle*"), StringLength(100)]
public string JobTitle { get; set; }
//--Cost Center ---//
[Required(ErrorMessage = "Please enter Cost Centre")]
[Display(Name = "Cost Centre*"), StringLength(100)]
public string CostCentre { get; set; }
//--Contractor Agency Name ---//
[Required(ErrorMessage = "Please enter Contractor Agency Name")]
[Display(Name = "Contractor Agency Name*"), StringLength(255)]
public string ContractorAgencyName { get; set; }
//--Start Date ---//
[Required(ErrorMessage = "Please select Start Date")]
[Display(Name = "Start Date*")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime Startdate { get; set; }
//--Supervisor Name ---//
[Required(ErrorMessage = "Please enter Supervisor Name")]
[Display(Name = "Supervisor Name*"), StringLength(255)]
public string SupervisorName { get; set; }
//--Supervisor Email ---//
[Required(ErrorMessage = "Please enter Supervisor Email")]
[Display(Name = "Supervisor Email*"), StringLength(255)]
public string SupervisorEmail { get; set; }
//--Compensation ---//
[Required(ErrorMessage = "Please enter Compensation")]
[Display(Name = "Compensation*"), StringLength(50)]
public string Compensation { get; set; }
//--Incentive Plan ---//
[Required(ErrorMessage = "Please select Incentive Plan")]
[Display(Name = "Incentive Plan*"), StringLength(50)]
public string IncentivePlan { get; set; }
//--Target Incentive ---//
[Required(ErrorMessage = "Please enter Target Incentive")]
[Display(Name = "Target Incentive*"), StringLength(50)]
public string TargetIncentive { get; set; }
[Display(Name = "Last Modified By")]
public string LastModifiedBy { get; set; }
[Display(Name = "Last Modified TimeStamp")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime LastModifiedTimestamp { get; set; }
}
}
答案 0 :(得分:0)
在将参数从视图传递到页面模型的过程中,此属性LastModifiedTimestamp
为null,即使已分配也是如此。分配不正确。因为 DateTime 会生成此html代码<input type="datetime-local" />
。
一个更好的解决方案是更改其类型。
<input asp-for="StaffDetails.LastModifiedTimestamp" class="form-control" type="datetime" value="@DateTime.Now"/>
这是SupervisorEmail
,需要添加。
<div class="form-group">
<label asp-for="StaffDetails.SupervisorEmail" class="control-label"></label>
<input asp-for="StaffDetails.SupervisorEmail" class="form-control" value="" />
<span asp-validation-for="StaffDetails.SupervisorEmail" class="text-danger"></span>
</div>