Home Part Validation of a Form/Model in asp
Reply: 0

Part Validation of a Form/Model in asp

user4111
1#
user4111 Published in April 21, 2018, 10:40 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:

  1. Is it a good Idea to make a part Validation of a ViewModel? If not, what are the alternatives?
  2. Should i separate my ViewModel in two ViewModels? If yes, how can i tell the ModelState which ViewModel he should validate?
  3. 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
<form method="post">
     <div class="form-group">
          <label asp-for="Model.Customer_Name" class="control-label"></label>
          <input asp-for="Model.Customer_Name" class="form-control" />
          <span asp-validation-for="Model.Customer_Name" class="text-danger"></span>
     </div>
     <div class="form-group">
          <label asp-for="Model.Company_Name" class="control-label"></label>
          <input asp-for="Model.Company_Name" class="form-control" />
          <span asp-validation-for="Model.Company_Name" class="text-danger"></span>
     </div>
     <div class="form-group">
          <input asp-page-handler="FirstStep" type="submit" value="Search" class="btn btn-default" />
     </div>
     <div class="form-group">
          <label asp-for="Model.Company_ID" class="control-label"></label>
          <input asp-for="Model.Company_ID" class="form-control" />
          <span asp-validation-for="Model.Company_ID" class="text-danger"></span>
     </div>
     <div class="form-group">
          <input asp-page-handler="SecondStep" type="submit" value="Submit" class="btn btn-default" />
     </div>
</form>

Create.cshtml.cs

public class CreateModel : PageModel
{
    [BindProperty]
    public CustomerViewModel Model { get; set; } = new CustomerViewModel();
    public async Task<IActionResult> OnPostFirstStepAsync()
    {
         // Should only Validate Customer_Name and Company_Name
        if (!ModelState.IsValid)
        {                   
            return Page();
        }
        // Do Something
        return Page();
    }

    public async Task<IActionResult> 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.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310227 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO