14

I have seen some posts relating to this but can't seem to get it to work. With the redirect I get a 'resource cannot be found error'.

I am trying to redirect to a Details page. There is an ID in element which I store as NestId that I want to eventually be able to pass to the View. Right now I just want to redirect to the details page, there isn't a model or anything attached to it. I just want the NestId to make it there so that I can use it to make more AJAX calls with it.

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 am going about this the right way but help would be appreciated, I've stalled on this for a while. Thanks!

4 Answers 4

32

If your click handler is successfully called then 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 given that the action method parameter is a string which is nullable, then if NestId == null, won't cause any exception at all, given that the ModelBinder won't complain about it.

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

3 Comments

This does seem to get the job done. Is there a way around hard coding the url like that?
You could just replace it by: @Url.Action("Details", "Artists",new { NestId = '+NestId+' })' as @ABs suggests. Also, consider accepting the answer that helped you in order to help others.
Yea, I was waiting to see if there was a solution that didn't hard code it. When I tried the option you and ABs said, I get an error (I commented on his below).
5

i used to do like 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" })))';

on javascript file

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

in this way i can able to pass the parameter values on demand..

by using @Html.Raw, url will not get amp; for parameters

1 Comment

I like this solution, very clean! :)
0

This would also work 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; 
        })

Comments

-1

redirect with query string

 $('#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; 
                })

2 Comments

I get an error around the NestId saying there is too many characters in character literal on the url...
you may try this, var url = '@ Url.Content("~/Artists/Details?NestId =' + NestId + '")'

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.