C#C
C#3y 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>

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);
    }

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?
Was this page helpful?