1
var url = 'http://localhost:55026/Home/getData';
var hiddendata = $("<input>").attr("name","snippet").attr('value',"content").attr('type', 'hidden')
var form = $('<form action="' + url + '" method="post" target="_blank" >'  + '</form>');
$(form).append(hiddendata);
$('body').append(form);
form.submit();


[HttpPost, ValidateInput(false)]
public ActionResult getData(string snippet)
{
}

After getting value from controller i need to change the url like http://localhost:55026instead of http://localhost:55026/Home/getData;

Is this possible?

Kalpana K
  • 59
  • 1
  • 10

2 Answers2

1

You can call the RedirectToAction method in your HttpPost action method.

[HttpPost, ValidateInput(false)]
public ActionResult getData(string snippet)
{
   //to do : Do something with posted data
   return RedirectToAction("Index","Home");
}

This will send a 302 response to the browser with the location header value as /Home/Index and the browser will make a new GET request to this location.

Assuming Home/Index is your default action as per the routing configuration.

If you want to show the data in Home/Index, you may pass it using querystring /TempData

With Querystring

return RedirectToAction("Index","Home",new {id=snippet});

Now in your index action, add a parameter called id and you can read the value there

public ActionResult Index(string id="")
{
  //check the value in id param. you may pass to your view as needed
}

With TempData

TempData["Snippet"] = snippet;
return RedirectToAction("Index","Home");

Now in your index action

public ActionResult Index()
{
  var id=TempData["Snippet"] as string;
  //check the value in id variable. you may pass to your view as needed
}
Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thanks for your response.Here i can't pass the data to view public ActionResult getData(string snippet) {return View("Index");} .I have tried as per your suggestion.but i can't pass the value to view.it diredctly redirected to 'http://localhost:55026' – Kalpana K Aug 02 '16 at 12:37
  • Your initial question was how to do the redirect, If you want to show the data, you need to do `return View()` and pass the data to the view as needed. The url is going to be `/Home/getData`. If you want the url to be just `yourSiteName`, you may consider replacing `getData` method with the `Index` action of `Home` controller. With the default routing configuration the url `yourSiteName`(without any controller/action) will be handled by `Home/Index` – Shyju Aug 02 '16 at 12:58
  • 1
    Thanks for your response – Kalpana K Aug 02 '16 at 13:04
1

You can try this:

Response.Redirect("~/");
Romy Mathews
  • 797
  • 7
  • 13
  • Thanks for your response. – Kalpana K Aug 02 '16 at 12:41
  • Sure.this is helped for redirect the link.but Here i can't pass the form data to view .please refer my code.public ActionResult getData(string snippet) {return View("Index");} .I have tried as per your suggestion.but i can't pass the value to view.it diredctly redirected to 'localhost:55026'; – Kalpana K Aug 02 '16 at 12:53
  • If it is a simple string or int value, you can store in Tempdata and access the same in the View? – Romy Mathews Aug 02 '16 at 12:57
  • Thanks for your response – Kalpana K Aug 02 '16 at 13:03