Home Part Validation of a Form/Model in asp

# Part Validation of a Form/Model in asp

user4111
1#
user4111 Published in July 21, 2018, 1:43 pm
 i have a few Questions about Validation in asp. Use Case: I have a Razor Page called Create. The Page have 2 Submit buttons. If i fill the first Part of the Form and submit it, i want a "Part" Validation of the two Attributes Customer_Name and Company_Name and not of Company_ID. After i had filled the second Part of the Form i want only a validation of the Attribute Company_Id. In the example below - I can simply remove one Attribute with ModelState.Remove("Model.Company_ID") and my Problem is solved. But i don't want to do this. Its perfect for few Attributes. But what if i have more? A other Way is that i use something like: if (ModelState["Model.Customer_Name"].ValidationState == ModelValidationState.Invalid && ModelState["Model.Company_Name"].ValidationState == ModelValidationState.Invalid) { return Page(); }  It works fine, but it Validate Company_ID and shows me a Error. So my Questions are: Is it a good Idea to make a part Validation of a ViewModel? If not, what are the alternatives? Should i separate my ViewModel in two ViewModels? If yes, how can i tell the ModelState which ViewModel he should validate? How can i validate in the first Step the Customer_Name and Company_Name and in the Second the Company_ID? Create.cshtml @page @model TestProject.Pages.Test.CreateModel
 Create.cshtml.cs public class CreateModel : PageModel { [BindProperty] public CustomerViewModel Model { get; set; } = new CustomerViewModel(); public async Task OnPostFirstStepAsync() { // Should only Validate Customer_Name and Company_Name if (!ModelState.IsValid) { return Page(); } // Do Something return Page(); } public async Task OnPostSecondStepAsync() { // Should only Validate Company_ID if (!ModelState.IsValid) { return Page(); } // Do Something return Page(); } }  ViewModel.cs public class CustomerViewModel { [Required, Display(Name = "Customer Name:")] public string Customer_Name{ get; set; } [Required, Display(Name = "Company Name:")] public string Company_Name{ get; set; } [Required, Display(Name = "Company ID:")] public string Company_ID{ get; set; } }  Thank you for your help!
 You need to login account before you can post.
Processed in 0.317147 second(s) , Gzip On .