Skip to content Skip to sidebar Skip to footer

Script For Window.location.href Error

this Q. is a continue of this question first qestion I have a form that I need to redirect to action with the form uploaded details (just like In stackoverflow) , but apparently be

Solution 1:

This should do it if your JavaScript is in the cshtml:

var id =1;
   var url = '@Url.Action("Index", "Events")';
   window.location.href = url +"/"+ id;

Based on your description however it seems that you are trying to call this from a .js file which has no idea about MVC helpers so instead you can try something like this:

var id = 1; 

var getUrl = window.location;
var baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];

window.location.href = baseUrl + '/Events/Index' + id;

To get the correct Id modify your action to return the Id e.g.

public JsonResult Create(LectureFormViewModel viewModel){
   returnJson(new { Id=lectureGig.Id });
}

then you can access this Id in JavaScript e.g.

success: function(data) {
   var id = data.Id;
 }

Solution 2:

If it is in a cshtml file you can use the code

window.location.href = '@Url.Action("Index", "Events")'+ "?id=1";

If you are in js file

window.location.href='/Events/Index?id=1'

Post a Comment for "Script For Window.location.href Error"