Pass View Name to PartialViewResult By Jquery in MVC












0















I need to pass Views names to PartialViewResult from Jquery, single Controller & single PartialViewResult method to use entire application,




working controller code:-




    public PartialViewResult addNew(string MenuId)
{
return PartialView(@"~/Views/Test/Add.cshtml");
}



html jquery method:-




  $('#btn_add').click(function (e) {
window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1;
});


this code working fine but why should we add-code the views name in controller rather pass views name from jquery method, it will be very easy to maintain and very easy method for code




Expected output:
controller code:-




    public PartialViewResult addNew(string MenuId, string ViewNames)
{
return PartialView("@~/"+ ViewNames);

//return PartialView(@"~/Views/Test/Add.cshtml");
}



Expected output:
html jquery method:-




  $('#btn_add').click(function (e) {
window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';

});


above code showing An error occurred while processing your request. can anybody share your ideas?..




I need to pass views name from jquery method with single PartialViewResult method in MVC Controller











share|improve this question





























    0















    I need to pass Views names to PartialViewResult from Jquery, single Controller & single PartialViewResult method to use entire application,




    working controller code:-




        public PartialViewResult addNew(string MenuId)
    {
    return PartialView(@"~/Views/Test/Add.cshtml");
    }



    html jquery method:-




      $('#btn_add').click(function (e) {
    window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1;
    });


    this code working fine but why should we add-code the views name in controller rather pass views name from jquery method, it will be very easy to maintain and very easy method for code




    Expected output:
    controller code:-




        public PartialViewResult addNew(string MenuId, string ViewNames)
    {
    return PartialView("@~/"+ ViewNames);

    //return PartialView(@"~/Views/Test/Add.cshtml");
    }



    Expected output:
    html jquery method:-




      $('#btn_add').click(function (e) {
    window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';

    });


    above code showing An error occurred while processing your request. can anybody share your ideas?..




    I need to pass views name from jquery method with single PartialViewResult method in MVC Controller











    share|improve this question



























      0












      0








      0








      I need to pass Views names to PartialViewResult from Jquery, single Controller & single PartialViewResult method to use entire application,




      working controller code:-




          public PartialViewResult addNew(string MenuId)
      {
      return PartialView(@"~/Views/Test/Add.cshtml");
      }



      html jquery method:-




        $('#btn_add').click(function (e) {
      window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1;
      });


      this code working fine but why should we add-code the views name in controller rather pass views name from jquery method, it will be very easy to maintain and very easy method for code




      Expected output:
      controller code:-




          public PartialViewResult addNew(string MenuId, string ViewNames)
      {
      return PartialView("@~/"+ ViewNames);

      //return PartialView(@"~/Views/Test/Add.cshtml");
      }



      Expected output:
      html jquery method:-




        $('#btn_add').click(function (e) {
      window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';

      });


      above code showing An error occurred while processing your request. can anybody share your ideas?..




      I need to pass views name from jquery method with single PartialViewResult method in MVC Controller











      share|improve this question
















      I need to pass Views names to PartialViewResult from Jquery, single Controller & single PartialViewResult method to use entire application,




      working controller code:-




          public PartialViewResult addNew(string MenuId)
      {
      return PartialView(@"~/Views/Test/Add.cshtml");
      }



      html jquery method:-




        $('#btn_add').click(function (e) {
      window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1;
      });


      this code working fine but why should we add-code the views name in controller rather pass views name from jquery method, it will be very easy to maintain and very easy method for code




      Expected output:
      controller code:-




          public PartialViewResult addNew(string MenuId, string ViewNames)
      {
      return PartialView("@~/"+ ViewNames);

      //return PartialView(@"~/Views/Test/Add.cshtml");
      }



      Expected output:
      html jquery method:-




        $('#btn_add').click(function (e) {
      window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';

      });


      above code showing An error occurred while processing your request. can anybody share your ideas?..




      I need to pass views name from jquery method with single PartialViewResult method in MVC Controller








      jquery model-view-controller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 7:54









      Wojciech Wirzbicki

      1,52711425




      1,52711425










      asked Jan 2 at 7:19









      ethirajethiraj

      346




      346
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The actual problem here is you're passing slashes as query string value without using percent encoding, as shown in redirection code below:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';


          The generated URL from above example will look like this, which is not a valid URL because slash is a reserved character in URL string, hence "An error occurred while processing your request" message showed up:



          /Home/addNew?MenuId=1&viewName=Views/Test/Add.cshtml => invalid trailing slashes


          To fix this issue, it is necessary to put @Url.Encode() helper so that the slashes are encoded as %2f instead:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&ViewNames=' + '@Url.Encode("Views/Test/Add.cshtml")';


          which generates URL-encoded query string:



          /Home/addNew?MenuId=1&ViewNames=Views%2fTest%2fAdd.cshtml => valid


          If you're passing the URL from server-side variable/viewmodel string properties, make sure that all special characters passed as query string are encoded first:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + @MenuId + '&ViewNames=' + '@Url.Encode(ViewName)';


          However, I think better to include file path inside action method and pass just viewname without extension if you want to load partial view from same folder, hence avoiding Url.Encode() usage:



          public ViewResult addNew(string MenuId, string ViewNames)
          {
          return View(@"~/Views/Test/" + ViewNames + ".cshtml");
          }


          Notes:



          1) window.location.href will reload the whole page, you must use ViewResult to do so. If you want to refresh partial view without reloading entire page, use AJAX callback instead.



          $('#btn_add').click(function (e) {
          $.ajax({
          url: '@Url.Action("addNew", "Home")',
          type: 'GET',
          data: { MenuId: 1, ViewNames: '@Url.Encode("Views/Test/Add.cshtml")' },
          success: function (result) {
          $('#targetElement').html(result);
          },
          error: function (xhr, status, err) {
          // error handling
          }
          });
          });


          2) The action method parameters are able to recognize percent encoding characters and revert them to original string.



          Related issue:



          Characters allowed in a URL






          share|improve this answer
























          • your answer is very pretty good, i have to test in development, my thinking always reduce the re-work on every day, so hereafter in MVC don't need multiple Controller

            – ethiraj
            Jan 2 at 9:27











          • single controller & single method is enough for entire MVC application

            – ethiraj
            Jan 2 at 9:28











          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%2f54002628%2fpass-view-name-to-partialviewresult-by-jquery-in-mvc%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The actual problem here is you're passing slashes as query string value without using percent encoding, as shown in redirection code below:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';


          The generated URL from above example will look like this, which is not a valid URL because slash is a reserved character in URL string, hence "An error occurred while processing your request" message showed up:



          /Home/addNew?MenuId=1&viewName=Views/Test/Add.cshtml => invalid trailing slashes


          To fix this issue, it is necessary to put @Url.Encode() helper so that the slashes are encoded as %2f instead:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&ViewNames=' + '@Url.Encode("Views/Test/Add.cshtml")';


          which generates URL-encoded query string:



          /Home/addNew?MenuId=1&ViewNames=Views%2fTest%2fAdd.cshtml => valid


          If you're passing the URL from server-side variable/viewmodel string properties, make sure that all special characters passed as query string are encoded first:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + @MenuId + '&ViewNames=' + '@Url.Encode(ViewName)';


          However, I think better to include file path inside action method and pass just viewname without extension if you want to load partial view from same folder, hence avoiding Url.Encode() usage:



          public ViewResult addNew(string MenuId, string ViewNames)
          {
          return View(@"~/Views/Test/" + ViewNames + ".cshtml");
          }


          Notes:



          1) window.location.href will reload the whole page, you must use ViewResult to do so. If you want to refresh partial view without reloading entire page, use AJAX callback instead.



          $('#btn_add').click(function (e) {
          $.ajax({
          url: '@Url.Action("addNew", "Home")',
          type: 'GET',
          data: { MenuId: 1, ViewNames: '@Url.Encode("Views/Test/Add.cshtml")' },
          success: function (result) {
          $('#targetElement').html(result);
          },
          error: function (xhr, status, err) {
          // error handling
          }
          });
          });


          2) The action method parameters are able to recognize percent encoding characters and revert them to original string.



          Related issue:



          Characters allowed in a URL






          share|improve this answer
























          • your answer is very pretty good, i have to test in development, my thinking always reduce the re-work on every day, so hereafter in MVC don't need multiple Controller

            – ethiraj
            Jan 2 at 9:27











          • single controller & single method is enough for entire MVC application

            – ethiraj
            Jan 2 at 9:28
















          0














          The actual problem here is you're passing slashes as query string value without using percent encoding, as shown in redirection code below:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';


          The generated URL from above example will look like this, which is not a valid URL because slash is a reserved character in URL string, hence "An error occurred while processing your request" message showed up:



          /Home/addNew?MenuId=1&viewName=Views/Test/Add.cshtml => invalid trailing slashes


          To fix this issue, it is necessary to put @Url.Encode() helper so that the slashes are encoded as %2f instead:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&ViewNames=' + '@Url.Encode("Views/Test/Add.cshtml")';


          which generates URL-encoded query string:



          /Home/addNew?MenuId=1&ViewNames=Views%2fTest%2fAdd.cshtml => valid


          If you're passing the URL from server-side variable/viewmodel string properties, make sure that all special characters passed as query string are encoded first:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + @MenuId + '&ViewNames=' + '@Url.Encode(ViewName)';


          However, I think better to include file path inside action method and pass just viewname without extension if you want to load partial view from same folder, hence avoiding Url.Encode() usage:



          public ViewResult addNew(string MenuId, string ViewNames)
          {
          return View(@"~/Views/Test/" + ViewNames + ".cshtml");
          }


          Notes:



          1) window.location.href will reload the whole page, you must use ViewResult to do so. If you want to refresh partial view without reloading entire page, use AJAX callback instead.



          $('#btn_add').click(function (e) {
          $.ajax({
          url: '@Url.Action("addNew", "Home")',
          type: 'GET',
          data: { MenuId: 1, ViewNames: '@Url.Encode("Views/Test/Add.cshtml")' },
          success: function (result) {
          $('#targetElement').html(result);
          },
          error: function (xhr, status, err) {
          // error handling
          }
          });
          });


          2) The action method parameters are able to recognize percent encoding characters and revert them to original string.



          Related issue:



          Characters allowed in a URL






          share|improve this answer
























          • your answer is very pretty good, i have to test in development, my thinking always reduce the re-work on every day, so hereafter in MVC don't need multiple Controller

            – ethiraj
            Jan 2 at 9:27











          • single controller & single method is enough for entire MVC application

            – ethiraj
            Jan 2 at 9:28














          0












          0








          0







          The actual problem here is you're passing slashes as query string value without using percent encoding, as shown in redirection code below:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';


          The generated URL from above example will look like this, which is not a valid URL because slash is a reserved character in URL string, hence "An error occurred while processing your request" message showed up:



          /Home/addNew?MenuId=1&viewName=Views/Test/Add.cshtml => invalid trailing slashes


          To fix this issue, it is necessary to put @Url.Encode() helper so that the slashes are encoded as %2f instead:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&ViewNames=' + '@Url.Encode("Views/Test/Add.cshtml")';


          which generates URL-encoded query string:



          /Home/addNew?MenuId=1&ViewNames=Views%2fTest%2fAdd.cshtml => valid


          If you're passing the URL from server-side variable/viewmodel string properties, make sure that all special characters passed as query string are encoded first:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + @MenuId + '&ViewNames=' + '@Url.Encode(ViewName)';


          However, I think better to include file path inside action method and pass just viewname without extension if you want to load partial view from same folder, hence avoiding Url.Encode() usage:



          public ViewResult addNew(string MenuId, string ViewNames)
          {
          return View(@"~/Views/Test/" + ViewNames + ".cshtml");
          }


          Notes:



          1) window.location.href will reload the whole page, you must use ViewResult to do so. If you want to refresh partial view without reloading entire page, use AJAX callback instead.



          $('#btn_add').click(function (e) {
          $.ajax({
          url: '@Url.Action("addNew", "Home")',
          type: 'GET',
          data: { MenuId: 1, ViewNames: '@Url.Encode("Views/Test/Add.cshtml")' },
          success: function (result) {
          $('#targetElement').html(result);
          },
          error: function (xhr, status, err) {
          // error handling
          }
          });
          });


          2) The action method parameters are able to recognize percent encoding characters and revert them to original string.



          Related issue:



          Characters allowed in a URL






          share|improve this answer













          The actual problem here is you're passing slashes as query string value without using percent encoding, as shown in redirection code below:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&viewname=' + 'Views/Test/Add.cshtml';


          The generated URL from above example will look like this, which is not a valid URL because slash is a reserved character in URL string, hence "An error occurred while processing your request" message showed up:



          /Home/addNew?MenuId=1&viewName=Views/Test/Add.cshtml => invalid trailing slashes


          To fix this issue, it is necessary to put @Url.Encode() helper so that the slashes are encoded as %2f instead:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + 1 + '&ViewNames=' + '@Url.Encode("Views/Test/Add.cshtml")';


          which generates URL-encoded query string:



          /Home/addNew?MenuId=1&ViewNames=Views%2fTest%2fAdd.cshtml => valid


          If you're passing the URL from server-side variable/viewmodel string properties, make sure that all special characters passed as query string are encoded first:



          window.location.href = '@Url.Action("addNew", "Home")?MenuId=' + @MenuId + '&ViewNames=' + '@Url.Encode(ViewName)';


          However, I think better to include file path inside action method and pass just viewname without extension if you want to load partial view from same folder, hence avoiding Url.Encode() usage:



          public ViewResult addNew(string MenuId, string ViewNames)
          {
          return View(@"~/Views/Test/" + ViewNames + ".cshtml");
          }


          Notes:



          1) window.location.href will reload the whole page, you must use ViewResult to do so. If you want to refresh partial view without reloading entire page, use AJAX callback instead.



          $('#btn_add').click(function (e) {
          $.ajax({
          url: '@Url.Action("addNew", "Home")',
          type: 'GET',
          data: { MenuId: 1, ViewNames: '@Url.Encode("Views/Test/Add.cshtml")' },
          success: function (result) {
          $('#targetElement').html(result);
          },
          error: function (xhr, status, err) {
          // error handling
          }
          });
          });


          2) The action method parameters are able to recognize percent encoding characters and revert them to original string.



          Related issue:



          Characters allowed in a URL







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 9:22









          Tetsuya YamamotoTetsuya Yamamoto

          17k42342




          17k42342













          • your answer is very pretty good, i have to test in development, my thinking always reduce the re-work on every day, so hereafter in MVC don't need multiple Controller

            – ethiraj
            Jan 2 at 9:27











          • single controller & single method is enough for entire MVC application

            – ethiraj
            Jan 2 at 9:28



















          • your answer is very pretty good, i have to test in development, my thinking always reduce the re-work on every day, so hereafter in MVC don't need multiple Controller

            – ethiraj
            Jan 2 at 9:27











          • single controller & single method is enough for entire MVC application

            – ethiraj
            Jan 2 at 9:28

















          your answer is very pretty good, i have to test in development, my thinking always reduce the re-work on every day, so hereafter in MVC don't need multiple Controller

          – ethiraj
          Jan 2 at 9:27





          your answer is very pretty good, i have to test in development, my thinking always reduce the re-work on every day, so hereafter in MVC don't need multiple Controller

          – ethiraj
          Jan 2 at 9:27













          single controller & single method is enough for entire MVC application

          – ethiraj
          Jan 2 at 9:28





          single controller & single method is enough for entire MVC application

          – ethiraj
          Jan 2 at 9:28




















          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%2f54002628%2fpass-view-name-to-partialviewresult-by-jquery-in-mvc%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







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules