Problems passing a string from one view to the controller ASP.NET MVC5












-1















I have a problem in the parameter query from my view to the controller



from this controller sent to the view



Controller1



 [HttpPost]
[ValidateAntiForgeryToken]
Public ActionRelieve DetailPlannelEmployee (int DeductionsId)
{


if (DeductionsId! = null)
{

IList <string> Deductionsp = new List <string> ();
for (int i = 0; i <DeductionsId.Length; i ++)
{
int idDeduction = DeductionsId [i];
var Deduction = db.Tbl_Deducciones.FirstOrDefault (t => t.DedId == idDeduction);
Deductionsp.Add (Deduction.DedId.ToString ());

}
ViewBag.Deduccionesp


already in the view



<table class =" table table-xxs datatable-responsive ">
<thead>
<tr>

foreach (var Deduction in (List <string>) ViewBag.Deduccionesp) // Foreach of the selected deductions
{

<th> Deduction </ th>

}

</ tr>
</ thead>

<tbody>


But I need to send that ViewBag.Deduccionesp from that view to another controller



What is this?



`<li> <a href="@Url.Action("ExportarExcel","Planilla", new {DeduccionesM = ViewBag.Deduccionesp })"> <i class =" icon-file-excel position-left "> </ i> Export an Excel </a> </ li>


and in the other controller I'm trying to receive it



Controller 2



`public void ExportExcel (IList <int> DeductionsM)
{

} `









share|improve this question





























    -1















    I have a problem in the parameter query from my view to the controller



    from this controller sent to the view



    Controller1



     [HttpPost]
    [ValidateAntiForgeryToken]
    Public ActionRelieve DetailPlannelEmployee (int DeductionsId)
    {


    if (DeductionsId! = null)
    {

    IList <string> Deductionsp = new List <string> ();
    for (int i = 0; i <DeductionsId.Length; i ++)
    {
    int idDeduction = DeductionsId [i];
    var Deduction = db.Tbl_Deducciones.FirstOrDefault (t => t.DedId == idDeduction);
    Deductionsp.Add (Deduction.DedId.ToString ());

    }
    ViewBag.Deduccionesp


    already in the view



    <table class =" table table-xxs datatable-responsive ">
    <thead>
    <tr>

    foreach (var Deduction in (List <string>) ViewBag.Deduccionesp) // Foreach of the selected deductions
    {

    <th> Deduction </ th>

    }

    </ tr>
    </ thead>

    <tbody>


    But I need to send that ViewBag.Deduccionesp from that view to another controller



    What is this?



    `<li> <a href="@Url.Action("ExportarExcel","Planilla", new {DeduccionesM = ViewBag.Deduccionesp })"> <i class =" icon-file-excel position-left "> </ i> Export an Excel </a> </ li>


    and in the other controller I'm trying to receive it



    Controller 2



    `public void ExportExcel (IList <int> DeductionsM)
    {

    } `









    share|improve this question



























      -1












      -1








      -1








      I have a problem in the parameter query from my view to the controller



      from this controller sent to the view



      Controller1



       [HttpPost]
      [ValidateAntiForgeryToken]
      Public ActionRelieve DetailPlannelEmployee (int DeductionsId)
      {


      if (DeductionsId! = null)
      {

      IList <string> Deductionsp = new List <string> ();
      for (int i = 0; i <DeductionsId.Length; i ++)
      {
      int idDeduction = DeductionsId [i];
      var Deduction = db.Tbl_Deducciones.FirstOrDefault (t => t.DedId == idDeduction);
      Deductionsp.Add (Deduction.DedId.ToString ());

      }
      ViewBag.Deduccionesp


      already in the view



      <table class =" table table-xxs datatable-responsive ">
      <thead>
      <tr>

      foreach (var Deduction in (List <string>) ViewBag.Deduccionesp) // Foreach of the selected deductions
      {

      <th> Deduction </ th>

      }

      </ tr>
      </ thead>

      <tbody>


      But I need to send that ViewBag.Deduccionesp from that view to another controller



      What is this?



      `<li> <a href="@Url.Action("ExportarExcel","Planilla", new {DeduccionesM = ViewBag.Deduccionesp })"> <i class =" icon-file-excel position-left "> </ i> Export an Excel </a> </ li>


      and in the other controller I'm trying to receive it



      Controller 2



      `public void ExportExcel (IList <int> DeductionsM)
      {

      } `









      share|improve this question
















      I have a problem in the parameter query from my view to the controller



      from this controller sent to the view



      Controller1



       [HttpPost]
      [ValidateAntiForgeryToken]
      Public ActionRelieve DetailPlannelEmployee (int DeductionsId)
      {


      if (DeductionsId! = null)
      {

      IList <string> Deductionsp = new List <string> ();
      for (int i = 0; i <DeductionsId.Length; i ++)
      {
      int idDeduction = DeductionsId [i];
      var Deduction = db.Tbl_Deducciones.FirstOrDefault (t => t.DedId == idDeduction);
      Deductionsp.Add (Deduction.DedId.ToString ());

      }
      ViewBag.Deduccionesp


      already in the view



      <table class =" table table-xxs datatable-responsive ">
      <thead>
      <tr>

      foreach (var Deduction in (List <string>) ViewBag.Deduccionesp) // Foreach of the selected deductions
      {

      <th> Deduction </ th>

      }

      </ tr>
      </ thead>

      <tbody>


      But I need to send that ViewBag.Deduccionesp from that view to another controller



      What is this?



      `<li> <a href="@Url.Action("ExportarExcel","Planilla", new {DeduccionesM = ViewBag.Deduccionesp })"> <i class =" icon-file-excel position-left "> </ i> Export an Excel </a> </ li>


      and in the other controller I'm trying to receive it



      Controller 2



      `public void ExportExcel (IList <int> DeductionsM)
      {

      } `






      javascript c# asp.net asp.net-mvc asp.net-mvc-4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 17:11







      Marco Eufragio

















      asked Nov 22 '18 at 15:51









      Marco EufragioMarco Eufragio

      176




      176
























          3 Answers
          3






          active

          oldest

          votes


















          0














          This is about routing.



          @Url.Action("actionName","controllerName", "routeValue")



          By default, it should be like "{controller=Home}/{action=Index}/{id?}"



          Ref. https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1



          For example,



          <a href="@Url.Action("Products","Details", 5)">...</a>


          And passing the parameter,id, to the controller.



          public IActionResult Details(int id) { ... }


          If you want to pass a string, for example,



          <a href="@Url.Action("ExportarExcel","Planilla", new { DeduccionesM = ViewBag.Deduccionesp })">...</a>


          And the controller would be like,



          public void ExportExcel (sting DeductionsM) { ... }


          The type of parameter referes to the type of route value and the name of parameter refers to the name of route value, so you can change the string to another type such as IList .






          share|improve this answer































            0














            You cannot pass complex data between actions in this way. I suggest you look at TempData which survives across consecutive HTTP requests.



            ViewData/ViewBag/TempData comparision blog






            share|improve this answer































              0














              So, this is a bit of untested code, but you should get the idea of what you can try,



              Create a form as form in the page you want to post(i.e the second page), the inside that form, do a forloop on your data



              @using(Html.BeginForm(........))
              {....
              @{
              var stringList = (List <string>) ViewBag.Deduccionesp;
              for(int i = 0; i < stringList.Count; i ++)
              {
              @Html.Hidden($"DeductionsM[{i}]",stringList[i])
              }
              }
              }


              Then at the end, your link would then post this form that will have the values bound to the default ModelBinder.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434485%2fproblems-passing-a-string-from-one-view-to-the-controller-asp-net-mvc5%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                This is about routing.



                @Url.Action("actionName","controllerName", "routeValue")



                By default, it should be like "{controller=Home}/{action=Index}/{id?}"



                Ref. https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1



                For example,



                <a href="@Url.Action("Products","Details", 5)">...</a>


                And passing the parameter,id, to the controller.



                public IActionResult Details(int id) { ... }


                If you want to pass a string, for example,



                <a href="@Url.Action("ExportarExcel","Planilla", new { DeduccionesM = ViewBag.Deduccionesp })">...</a>


                And the controller would be like,



                public void ExportExcel (sting DeductionsM) { ... }


                The type of parameter referes to the type of route value and the name of parameter refers to the name of route value, so you can change the string to another type such as IList .






                share|improve this answer




























                  0














                  This is about routing.



                  @Url.Action("actionName","controllerName", "routeValue")



                  By default, it should be like "{controller=Home}/{action=Index}/{id?}"



                  Ref. https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1



                  For example,



                  <a href="@Url.Action("Products","Details", 5)">...</a>


                  And passing the parameter,id, to the controller.



                  public IActionResult Details(int id) { ... }


                  If you want to pass a string, for example,



                  <a href="@Url.Action("ExportarExcel","Planilla", new { DeduccionesM = ViewBag.Deduccionesp })">...</a>


                  And the controller would be like,



                  public void ExportExcel (sting DeductionsM) { ... }


                  The type of parameter referes to the type of route value and the name of parameter refers to the name of route value, so you can change the string to another type such as IList .






                  share|improve this answer


























                    0












                    0








                    0







                    This is about routing.



                    @Url.Action("actionName","controllerName", "routeValue")



                    By default, it should be like "{controller=Home}/{action=Index}/{id?}"



                    Ref. https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1



                    For example,



                    <a href="@Url.Action("Products","Details", 5)">...</a>


                    And passing the parameter,id, to the controller.



                    public IActionResult Details(int id) { ... }


                    If you want to pass a string, for example,



                    <a href="@Url.Action("ExportarExcel","Planilla", new { DeduccionesM = ViewBag.Deduccionesp })">...</a>


                    And the controller would be like,



                    public void ExportExcel (sting DeductionsM) { ... }


                    The type of parameter referes to the type of route value and the name of parameter refers to the name of route value, so you can change the string to another type such as IList .






                    share|improve this answer













                    This is about routing.



                    @Url.Action("actionName","controllerName", "routeValue")



                    By default, it should be like "{controller=Home}/{action=Index}/{id?}"



                    Ref. https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1



                    For example,



                    <a href="@Url.Action("Products","Details", 5)">...</a>


                    And passing the parameter,id, to the controller.



                    public IActionResult Details(int id) { ... }


                    If you want to pass a string, for example,



                    <a href="@Url.Action("ExportarExcel","Planilla", new { DeduccionesM = ViewBag.Deduccionesp })">...</a>


                    And the controller would be like,



                    public void ExportExcel (sting DeductionsM) { ... }


                    The type of parameter referes to the type of route value and the name of parameter refers to the name of route value, so you can change the string to another type such as IList .







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 11:16









                    Wing Kui TsoiWing Kui Tsoi

                    154213




                    154213

























                        0














                        You cannot pass complex data between actions in this way. I suggest you look at TempData which survives across consecutive HTTP requests.



                        ViewData/ViewBag/TempData comparision blog






                        share|improve this answer




























                          0














                          You cannot pass complex data between actions in this way. I suggest you look at TempData which survives across consecutive HTTP requests.



                          ViewData/ViewBag/TempData comparision blog






                          share|improve this answer


























                            0












                            0








                            0







                            You cannot pass complex data between actions in this way. I suggest you look at TempData which survives across consecutive HTTP requests.



                            ViewData/ViewBag/TempData comparision blog






                            share|improve this answer













                            You cannot pass complex data between actions in this way. I suggest you look at TempData which survives across consecutive HTTP requests.



                            ViewData/ViewBag/TempData comparision blog







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 16:10









                            Steve0Steve0

                            1,7282922




                            1,7282922























                                0














                                So, this is a bit of untested code, but you should get the idea of what you can try,



                                Create a form as form in the page you want to post(i.e the second page), the inside that form, do a forloop on your data



                                @using(Html.BeginForm(........))
                                {....
                                @{
                                var stringList = (List <string>) ViewBag.Deduccionesp;
                                for(int i = 0; i < stringList.Count; i ++)
                                {
                                @Html.Hidden($"DeductionsM[{i}]",stringList[i])
                                }
                                }
                                }


                                Then at the end, your link would then post this form that will have the values bound to the default ModelBinder.






                                share|improve this answer




























                                  0














                                  So, this is a bit of untested code, but you should get the idea of what you can try,



                                  Create a form as form in the page you want to post(i.e the second page), the inside that form, do a forloop on your data



                                  @using(Html.BeginForm(........))
                                  {....
                                  @{
                                  var stringList = (List <string>) ViewBag.Deduccionesp;
                                  for(int i = 0; i < stringList.Count; i ++)
                                  {
                                  @Html.Hidden($"DeductionsM[{i}]",stringList[i])
                                  }
                                  }
                                  }


                                  Then at the end, your link would then post this form that will have the values bound to the default ModelBinder.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    So, this is a bit of untested code, but you should get the idea of what you can try,



                                    Create a form as form in the page you want to post(i.e the second page), the inside that form, do a forloop on your data



                                    @using(Html.BeginForm(........))
                                    {....
                                    @{
                                    var stringList = (List <string>) ViewBag.Deduccionesp;
                                    for(int i = 0; i < stringList.Count; i ++)
                                    {
                                    @Html.Hidden($"DeductionsM[{i}]",stringList[i])
                                    }
                                    }
                                    }


                                    Then at the end, your link would then post this form that will have the values bound to the default ModelBinder.






                                    share|improve this answer













                                    So, this is a bit of untested code, but you should get the idea of what you can try,



                                    Create a form as form in the page you want to post(i.e the second page), the inside that form, do a forloop on your data



                                    @using(Html.BeginForm(........))
                                    {....
                                    @{
                                    var stringList = (List <string>) ViewBag.Deduccionesp;
                                    for(int i = 0; i < stringList.Count; i ++)
                                    {
                                    @Html.Hidden($"DeductionsM[{i}]",stringList[i])
                                    }
                                    }
                                    }


                                    Then at the end, your link would then post this form that will have the values bound to the default ModelBinder.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 22 '18 at 16:15









                                    mahlatsemahlatse

                                    1,012519




                                    1,012519






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434485%2fproblems-passing-a-string-from-one-view-to-the-controller-asp-net-mvc5%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        這個網誌中的熱門文章

                                        Xamarin.form Move up view when keyboard appear

                                        Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                        Anylogic : not able to use stopDelay()