C
C#12mo ago
Alizer

❔ Return PartialView with ``for`` attribute

I have a complex <form> in my .cshtml file and I want to simplify it more, now I have this original code, this is inside the <form>
<div class="container-fluid mx-0 px-0">
@foreach (var assistanceData in i.Assistances)
{
var assistanceDataIndex = i.Assistances.IndexOf(assistanceData);
<p class="mb-1 fw-normal">@assistanceData.Name</p>
<partial for="Assistances[assistanceIndex].Assistances[assistanceDataIndex]" name="Encoder/AssistanceDataForm"/>
}
</div>
<div class="container-fluid mx-0 px-0">
@foreach (var assistanceData in i.Assistances)
{
var assistanceDataIndex = i.Assistances.IndexOf(assistanceData);
<p class="mb-1 fw-normal">@assistanceData.Name</p>
<partial for="Assistances[assistanceIndex].Assistances[assistanceDataIndex]" name="Encoder/AssistanceDataForm"/>
}
</div>
now instead of doing a foreach loop I want to add these <partial/> views with the click of a button, I plan on doing this by making a controller return the partial view and I'll just append it to the parent div via an ajax call using jquery, I have the following action in the controller below
c#
public async Task<IActionResult> AssistanceDataForm(Assistance parentAssistance, int assistanceIndex, int assistanceDataIndex)
{
return PartialView("Encoder/AssistanceDataForm", assistanceData);
}
c#
public async Task<IActionResult> AssistanceDataForm(Assistance parentAssistance, int assistanceIndex, int assistanceDataIndex)
{
return PartialView("Encoder/AssistanceDataForm", assistanceData);
}
this returns the partial view but how do I add for="parentAssistance[assistanceIndex].Assistances[assistanceDataIndex]" in the PartialView? Is this even possible? If not could there be another way around it?
5 Replies
Angius
Angius12mo ago
I'd just see what that asp-for generates and append all the elements needed with JS ¯\_(ツ)_/¯
Alizer
Alizer12mo ago
yeah but isnt there a more better way or maybe we can do this with blazor
Angius
Angius12mo ago
If you want to pull a whole-ass Blazor to do that, sure, you can try
Alizer
Alizer12mo ago
mmm...i see, if its tanya saying it then there really must be no other way
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts