I've got number of places where I need to use an address in my app so I tried to make this DRY and create an address partial view like this:
@model AddressEditViewModel
<div >
<label asp-for="Address1" ></label>
<input asp-for="Address1" />
<span asp-validation-for="Address1" ></span>
</div>
<div >
<label asp-for="Address2" ></label>
<input asp-for="Address2" />
<span asp-validation-for="Address2" ></span>
</div>
<div >
<label asp-for="City" ></label>
<input asp-for="City" />
<span asp-validation-for="City" ></span>
</div>
<div >
<div >
<label asp-for="USStateID" ></label>
<select asp-items="@Model.USStates" asp-for="USStateID" ></select>
<span asp-validation-for="USStateID" ></span>
</div>
<div >
<label asp-for="Zip" ></label>
<input asp-for="Zip" />
<span asp-validation-for="Zip" ></span>
</div>
</div>
But I had no luck when I put it into another page with the partial tag helper:
<partial name="_Address" model="Model.Address" />
Now I understand why this technique doesn't work - that partial views are just for making HTML and don't get processed by the model binder on the way back.
But I would prefer not to copy and paste forms all over my site. Is there any better way to reuse commonly used form bits like this?
CodePudding user response:
You can use this method.
@await Html.PartialAsync("_PartialName", customViewData)
You can find more details in here.