Kalpana K Kalpana K - 4 months ago 10
jQuery Question

How can i change the url after form post

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:55026
instead of
http://localhost:55026/Home/getData
;

Is this possible?

Answer

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
}
Comments