4 Replies - 462 Views - Last Post: 13 June 2019 - 11:47 AM Rate Topic: -----

#1 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 7987
  • View blog
  • Posts: 12,406
  • Joined: 28-September 07

Passing variables for dynamic naming

Posted 13 June 2019 - 08:59 AM

I'm building a web app (using .NET MVC Core 2) that has multiple separate components displaying on the same page. I need to be able to pass a specific variable from one to the other without the page reloading.

This is the navigation code (file: Views/Shared/Components/Navigation/Default.cshtml)
@model IEnumerable<ModuleUserGroup>

<div id="left_nav">
     @if(Model.Count() > 0) {
          foreach(var m in Model){
               <button class="accordion"><h2>@m.Module.Name</h2></button>
               <div class="panel" style="display:none;">
               @if(m.Module.Sections.Count() > 0){
                    foreach(var s in m.Module.Sections){
                         string A = "submodule";
                         string str;
                         str = String.Concat(A, s.Id);
                         <p>
                              <a [email protected] href="#">@s.Name</a>
                         </p>
                    }
               }
               else {
                    <p>No current submodules</p>
               }
          </div>
     }
}



This is the content pane (file: Views/Module/Index.cshtml)
<script type="text/javascript">
     $(document).ready(function(){
          var p2 = "<h2>Option 1</h2><p>Lorem ipsum dolor sit amet.</p>";
          var p3 = "<h2>Option 2</h2><p>Lorem ipsum dolor sit amet.</p>";
          $("#submodule2").click(function(){
               $(".content-container").html(p2);
          });
          $("#submodule3").click(function(){
               $(".content-container").html(p3);
          });
      });
</script>

<div class='col-md-3">
     @await Component.InvokeAsync("Navigation")
</div>

<div class="content-container">
     <h2>Changeable content pane.</h2>
</div>



How do I get the value of s.Id in the navigation file over to the content pane file without passing it through the URL? It will be used to pull content from a database to display in the content pane.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing variables for dynamic naming

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: Passing variables for dynamic naming

Posted 13 June 2019 - 09:11 AM

Is this a full 'view' or a 'partial view'?

Though passing data between views is typically up to: ViewBag, ViewData or TempData.
https://rachelappel....3-applications/

Alternatively you could try the perilous ajax route to reload a view.
Ex:
https://stackoverflo...net-for-a-mvc-p
Was This Post Helpful? 0
  • +
  • -

#3 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 7987
  • View blog
  • Posts: 12,406
  • Joined: 28-September 07

Re: Passing variables for dynamic naming

Posted 13 June 2019 - 10:59 AM

Partial view.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Passing variables for dynamic naming

Posted 13 June 2019 - 11:07 AM

I recommend the AJAX + partial view approach.
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is offline

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,108
  • Joined: 12-January 10

Re: Passing variables for dynamic naming

Posted 13 June 2019 - 11:47 AM

you can always use a session variable if it comes to that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1