0

I am working on an Asp.Net-Core Razor Page app and I am trying to send from java script some variables to use in my Model. Here is my js code:

 const val1 = new Date(document.getElementById('val1').value);
 const val2 = new Date(document.getElementById('val2').value);

 location.href = "./MyPage/SaveValues" + "&val1=" + val1 + "&val2=" + val2;

In my model:

public void OnGetSaveValues(string value1, string value2)
{
    
}

The problem is that I receive an error with localhost can't be found. How can I use the values from the url in my model?

1 Answer 1

1

In JavaScript, you don't use the ./MyPage which you use to get a razor page route with base url. In JavaScript, you need to retrieve the base url from the window. Without the base URL appended to the route, the browser will always tell you not found because the path is incorrect. This should be what you should be doing


 location.href = window.location.origin + "/MyPage/SaveValues/?value1=" + value1 + "&value2=" + value2;
Sign up to request clarification or add additional context in comments.

8 Comments

You are right now I am redirected on the page. But how do I access now the variables from the url? Should I have something in my Model View like: @page "{value1}"
Take a look at the updated code. Don't change your controller action as you pasted in your question and then use the code in my answer and everything should work fine
I did the exact thing but If I add a Debug.WriteLine(value1) nothing will be printed. Somehow my value is not getting in the OnGet method
Did you put that question mark in the route? It's important
Ies I did add the question mark. The redirect to the page works fine, I can see the url with the right parameters. The problem is that I can't access the values in my OnGet method
|

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.