MVC Redirects to view from jQuery with parameters

I saw some posts related to this, but it can't seem to make it work. When redirecting, I get "the resource could not be found by error."

I am trying to redirect to the details page. The element has an identifier that I store as NestId, which I want to eventually pass to the view. Right now I just want to redirect to the details page, there is no model or something like that. I just want NestId to do it there so that I can use it to create more AJAX calls.

Here is my jQuery:

$('#results').on('click', '.item', function () { var NestId = $(this).data('id'); var url = '@Url.Action("Details, Artists")'; window.location.href = url; }) 

Here is the function on the controller:

 public ActionResult Details(string NestId) { ViewBag.NestId = NestId; return View(); } 

I'm not sure if I will do it right, but help will be appreciated, I am a bit stumped. Thanks!

+7
javascript jquery asp.net-mvc url-redirection
source share
4 answers

If your click handler is called successfully, this should work:

 $('#results').on('click', '.item', function () { var NestId = $(this).data('id'); var url = "/Artists/Details?NestId=" + NestId; window.location.href = url; }) 

EDIT: In this particular case, if the action method parameter is a string that is null, then if NestId == null will not raise any exceptions at all, given that ModelBinder will not complain about it.

+19
source share

I used for this

inside view

 <script type="text/javascript"> //will replace the '_transactionIds_' and '_payeeId_' var _addInvoiceUrl = '@(Html.Raw( Url.Action("PayableInvoiceMainEditor", "Payables", new { warehouseTransactionIds ="_transactionIds_",payeeId = "_payeeId_", payeeType="Vendor" })))'; 

in javascript file

 var url = _addInvoiceUrl.replace('_transactionIds_', warehouseTransactionIds).replace('_payeeId_', payeeId); window.location.href = url; 

this way I can pass parameter values ​​on request.

using @ Html.Raw url won't get for parameters

+2
source share

This will work too, I believe:

 $('#results').on('click', '.item', function () { var NestId = $(this).data('id'); var url = '@Html.Raw(Url.Action("Artists", new { NestId = @NestId }))'; window.location.href = url; }) 
0
source share

query string redirection

  $('#results').on('click', '.item', function () { var NestId = $(this).data('id'); // var url = '@Url.Action("Details", "Artists",new { NestId = '+NestId+' })'; var url = '@ Url.Content("~/Artists/Details?NestId =' + NestId + '")' window.location.href = url; }) 
-one
source share

All Articles