Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:29:27+00:00 2026-05-28T00:29:27+00:00

I have a Razor view with several fields rendered from a View model, and

  • 0

I have a Razor view with several fields rendered from a View model, and I need the same fields to appear in different sections of an accordion (I don’t want to duplicate the fields in each section). I’m not sure of the best way to do this – do I put the fields in a partial view and somehow render/load the view dynamically when an accordion section is clicked, or after the code is rendered do I move/append the div containing the fields, or something else completely?

EDIT: Check out jsfiddle here

Razor code:

<div id="accordion">
    <h3><a href="#" class="acc-section">First header</a></h3>
    <div id="div1">
        <fieldset>
            @Html.LabelFor(model => defaultStudyEvent.GroupName, "Group Name")
            @Html.TextBoxFor(model => defaultStudyEvent.TimePoints)
        </fieldset>
    </div>
    <h3><a href="#" class="acc-section">Second header</a></h3>
    <div></div>
</div>

Rendered html:

<div id="accordion">
    <h3><a href="#" class="acc-section">First header</a></h3>
    <div id="div1">
        <fieldset id="f1">
            <label for="defaultStudyEvent_GroupName">Group Name</label>
            <input id="defaultStudyEvent_TimePoints" name="defaultStudyEvent.TimePoints" type="text" value="" />
        </fieldset>
    </div>
    <h3><a href="#" class="acc-section">Second header</a></h3>
    <div>(Move fieldset above to here when "Second header" is clicked)</div>
</div>

I’ve been playing with various options such as below but I’m not sure if this is the correct approach:

$(document).ready(function () {
    $('#accordion').accordion();
    $('.acc-section').click(appendFields);
});

function appendFields() {
    $('#accordion')
        .append($('#div1'))
        .accordion('destroy')
        .accordion();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:29:28+00:00Added an answer on May 28, 2026 at 12:29 am

    Ah, now I understand the problem, based on the above comments. Sorry if my initial questions confused you. 🙂

    I think you are on the right track – the following worked fine for me:

    HTML

    <div id="accordion">
        <h3 class="acc-header"><a href="#">First header</a></h3>
        <div>
            <fieldset id="input-fields">
                <label for="defaultStudyEvent_GroupName">Group Name</label>
                <input id="defaultStudyEvent_TimePoints" name="defaultStudyEvent.TimePoints" type="text" value="" />
            </fieldset>
        </div>
        <h3 class="acc-header"><a href="#">Second header</a></h3>
        <div>
        </div>
        <!-- more divs can be added -->
    </div>
    

    jQuery:

    $(document).ready(function () {
        $('#accordion').accordion();
        $('.acc-header').click(moveFields);
    
        function moveFields() {
            $(this).next().append($('#input-fields'));
        }
    });
    

    Note that I added my own class for .acc-header. While you could use the built in jQuery UI classes, using your own makes sure this won’t break if a new UI version changes the class naming (not very likely, but still).

    The above solution will make sure that you only ever use one set of the input fields, so you won’t receive any duplicate post data. The nifty thing with .append() is that it moves the thing you append, so you don’t have to remove anything afterwards.

    As for “correct approach” I can’t say – but it seems like the cleanest way to do it if you ask me. Another possible approach would be to create the same fields for all accordion panes in the view, and in some way disable everyone except the ones in the active pane. Would need more JavaScript for that, however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the razor view engine, I have some code like the following: @Html.TextAreaFor(model =>
I have a need to learn MVC 3 with the Razor view engine and
I'm converting from aspx view to razor. In the view I have the following
So I have a razor view: @{ int i=1; foreach(var story in Model.Storylines) {
I have created one page in MVC 3.0 Razor view. Create.cshtml @model LiveTest.Business.Models.QuestionsModel @*<script
I have a @model MyModel on a razor view. I know if I have
I have a razor view with a BeginForm command and a loop within that
I am working on asp.net MVC 3 application. I have created a Razor view
Possible Duplicate: Emitting unencoded strings in a Razor view I have a string that
I have created a Razor view in my asp.net MVC 3 application. It is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.