[javascript] MVC Redirect to View from jQuery with parameters

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!

This question is related to javascript jquery asp.net-mvc url-redirection

The answer is


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; 
                })

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


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; 
        })

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to asp.net-mvc

Using Lato fonts in my css (@font-face) Better solution without exluding fields from Binding Vue.js get selected option on @change You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to send json data in POST request using C# VS 2017 Metadata file '.dll could not be found The default XML namespace of the project must be the MSBuild XML namespace How to create roles in ASP.NET Core and assign them to users? The model item passed into the dictionary is of type .. but this dictionary requires a model item of type How to use npm with ASP.NET Core

Examples related to url-redirection

How to set the DefaultRoute to another Route in React Router MVC Redirect to View from jQuery with parameters Go to URL after OK button if alert is pressed How do you redirect to a page using the POST verb?