How to hide URL parameters in MVC4

http://localhost:49397/ChildCare/SponsorChild/83

This is the link that is generated when I click on the link to the action in the table and redirect to the "Edit Action" action, now I want to hide the number "83" in the URL, how can I do it,

I am using VS2010 MVc4 Razor, sorry for my poor ownership in advance thanks

+4
source share
3 answers

If you work with links, links are sent at the GET request to the server, then the parameters are specified in the URL. Perhaps you have two options:

1 - the parameters should be on the data attributes, such as data-id="83" , and then create a form for sending data by mail and create input tags with data-x attributes, for example:

 <a href="my/url" data-id="83> link </a> 

then using javascript you need to create a form:

 <form method="POST" action="my/url">    <input value="83 name="id" type="hidden" /> </form> 

and fire the event with a JS submit form: jQuery('form').submit()

2 - you can encrypt and then decrypt the receive parameters in the controller: How to encrypt and decrypt data in MVC?

Edit

An example for the first paragraph:

Html:

 <div id="container-generic-form" style="display:none;"> <form action="" method="POST"></form> </div> <a href="my/url" data-id="83" data-other="blue" class="link-method-post">my link</a> 

JS:

 $(function() { // document ready var controlAnchorClickPost = function(event) { event.preventDefault(); // the default action of the event will not be triggered var data = $(this).data(), form = $('#container-generic-form').find('form'); for(var i in data) { var input = $('<input />', { type: 'hidden', name: i }).val(data[i]); input.appendTo(form); } form.submit(); }; $('a.link-method-post').on('click', controlAnchorClickPost); //jquery 1.7 }); 
+5
source

We use two pages to hide the variable.

 public ActionResult RestoreSavedSession(string id) { Session["RestoreSavedSession"] = id; return RedirectToAction("RestoreSavedSessionValidation"); } public ActionResult RestoreSavedSessionValidation() { return View("RestoreSavedSessionValidation"); } 

You click RestoreSavedSession , then it saves the parameters locally and calls RestoreSavedSessionValidation , where it reads the parameter from Session or Cache or something else.

+3
source

I use the preview method to save the route data in TempData and direct them to the correct action.

  public async Task<ActionResult> Preview(string act, string ctl, string obj) { TempData["Data"] = obj; return RedirectToAction(act, ctl); } 

To use it

 return RedirectToAction("Preview","Controller",new {act="action",ctl="controller",obj=JsonConvet.SerializeObject(obj)}); 

After routing

 var x=JsonConvert.DeserializeObject<T>(TempData["Data"].ToString()); 
+1
source

All Articles