Search code examples
asp.net-corerazorasp.net-core-tag-helpers

How to get full namespace path of model with asp-for


I am currently writing one-page checkout functionality for am e-commerce project. This functionality has Register and Guest tabs in a Form field and both have address fields like street, firstname, lastname, etc.. I am separating them by using a radio input

<input type="radio" hidden value="guest" asp-for="AuthModule.AuthType" />

This AuthModule class has both Register and Guest as class named "Register"

  public partial class AuthModule
  {
      public Register Register { get; set; }
      public Register Guest { get; set; }
      public string AuthType { get; set; }
  }  

I also have a partial view named InvoiceAddress.cshtml in there I am using bunch of inputs for Firstname, Lastname, Street, etc... However I want this InvoiceAddress partial both available for Register and Guest which makes things messy like same inputs for different properties which makes validation hard, so I thought I can separete them by namespacing them like :

<label asp-for="InvoiceAddress.Firstname">@Localizer["deliveryAddressFormFirstname"]</label>

which outputs as :

<input type="text" class="form-control is-required" id="InvoiceAddress_Firstname" name="InvoiceAddress.Firstname">

since both Register and Guest has the same Model given @model type in partial view results only InvoiceAddress.Firstname as input name but I need to make this name full path like AuthModule.Register.InvoiceAddress.Firstname


Solution

  • I think what you're looking for is the for attribute on PartialTagHelper:

    <partial name="_InvoiceAddress" for="Register.InvoiceAddress" />