7

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

This is the Link ,which is being generated when i click on action link in table and redirecting to Edit Action, now i want to Hide the number '83' in the URL how can i acheive this,

i am using VS2010 MVc4 Razor, Sorry for my bad engllish thanks in advance

4
  • Thanks for quick reply ,Can u explain me in detail Commented Feb 12, 2013 at 6:51
  • 2
    Use POST rather than GET Commented Feb 12, 2013 at 8:10
  • 2
    Please provide more information, otherwise people have to guess what you want. Commented Feb 12, 2013 at 8:35
  • 1
    You should not hide them Commented Apr 19, 2013 at 14:31

3 Answers 3

6

if you work with links, the links send by GET request to the server, then the parameters are in the url. Might you have two options:

1 - the parameters would have to be on data attributes like data-id="83" and then create a form to send data by post, and creating tags input with attributes data-x, for example:

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

then with javascript you need create the form:

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

and run the event with JS form submit like: jQuery('form').submit()

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

Edit

Example for point one:

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

});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the Reply, found answer at the given link, i don't how to work with data- attributes.i Used Encoding.UTF8.GetBytes() encode , decode methods. thanks again
5

We use Two pages like that to hide the variable

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

    public ActionResult RestoreSavedSessionValidation()
    {
        return View("RestoreSavedSessionValidation");
    }

You hit RestoreSavedSession it then takes parameter stores it locally and calls RestoreSavedSessionValidation where it reads parameter from Session or Cache or whatever.

1 Comment

this is a nice and simple solution, I think.
1

I uses a preview method store the route data to TempData, and route it 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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.