Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get model property name in a partial view

I have an ASP.NET Core partial view that solicits (for example) a person's first and last name. The partial view takes this view model:

// MyApp.NamePartialViewModel.cs
public class NamePartialViewModel{
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

The partial view renders input elements for its view model, like this (obviously, I've omitted labels and unobtrusive validation stuff):

@* NamePartialView.cshtml *@
@model MyApp.NamePartialViewModel
<input asp-for="FirstName" />
<input asp-for="LastName>" />

Now, the partial view is referenced in a parent view. For the sake of a reasonably complete example, I'll use the partial view to get names for two different people, so the view model looks like this:

// MyApp.MyViewModel.cs
public class MyViewModel {
  public NamePartialViewModel FirstPersonName { get; set; }
  public NamePartialViewModel SecondPersonName { get; set; }
}

And, finally, the parent view (displayed by the controller) looks like this:

@model MyApp.MyViewModel
<form method="post">
  <partial name="NamePartialView" model="@Model.FirstPersonName" />
  <partial name="NamePartialView" model="@Model.SecondPersonName" />
</form>

The point here is that each <partial> element feeds an instance of NamePartialViewModel to the partial view.

My question is: Is there a way, in the partial view (or in code called from the partial view) to get the name of the property bound to the Model? In other words, I want to discover that (in this example) my model is bound to a property named either FirstPersonName or SecondPersonName (that is, the name of the property in the parent view model).

So, why do I need this, you ask? Because in order for the model binding in the controller POST routine to work correctly, I need to prefix the <input> element's name attribute with the property name, so the first partial view invocation would actually render something like this:

<input name="FirstPersonName.FirstName" id="FirstPersonName_FirstName" />
<input name="FirstPersonName.LastName" id="FirstPersonName_LastName" />

so I need some code that can fabricate the prefix based on the name of the property in the parent view model that becomes the view model for the partial view.

like image 541
Bob.at.Indigo.Health Avatar asked Sep 14 '25 23:09

Bob.at.Indigo.Health


1 Answers

Using partial tag helper when you pass a model using for, the property name will be accessible through ViewData.TemplateInfo.HtmlFieldPrefix in the partial view.

As long as you use standard helpers you don't need to use ViewData.TemplateInfo.HtmlFieldPrefix, however as an author of a tag helper or as the one who wants to use manual tag renderings, you need to care about it.

NamePartialView.cshtml

@model MyApp.NamePartialViewModel

<label asp-for="FirstName" />
<input asp-for="FirstName" />
<label asp-for="LastName" />
<input asp-for="LastName" />

Just in case of creating manual tags, here is the prefix to use:
@ViewData.TemplateInfo.HtmlFieldPrefix

MyViewModel.cshtml

@model MyApp.MyViewModel

<partial name="NamePartialView" for="FirstPersonName" />
<partial name="NamePartialView" for="SecondPersonName" />
like image 186
Reza Aghaei Avatar answered Sep 16 '25 15:09

Reza Aghaei