© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
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
    public async Task<IActionResult> AssistanceDataForm(Assistance parentAssistance, int assistanceIndex, int assistanceDataIndex)
    {
        return PartialView("Encoder/AssistanceDataForm", assistanceData);
    }
    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]"
for="parentAssistance[assistanceIndex].Assistances[assistanceDataIndex]"
in the PartialView? Is this even possible? If not could there be another way around it?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Apply multiple attributes from custom attribute
C#CC# / help
2y ago
Set a class attribute result with a custom attribute
C#CC# / help
3y ago
Identifying something with an attribute with IIncrementalSourceGenerator
C#CC# / help
2y ago