2

I am trying to get two query string parameters from JavaScript to the controller. Here is the code:

JavaScript

var startDate = "",
    enddate = "";

var startDate = $.datepicker.formatDate(dateFormat, $("#startDate").datepicker('getDate'));
var enddate = $.datepicker.formatDate(dateFormat, $("#endDate").datepicker('getDate'));

if (startDate != "" || enddate != "") {
  window.location = `${window.location.href}/Index?startDate=${startDate}&endDate=${enddate}`;

}

C#

public IActionResult Index([FromQuery(Name = "startDate")] string startDate = "", [FromQuery(Name = "endDate")] string endDate = "") 
{    
}

The controller gets called, and the first parameter is fin but the second parameter gets a messed up version of the URL. Here is a picture. I am having a hard time figuring out what I am doing wrong.

JavaScript Values

enter image description here

C# values

enter image description here

5
  • What are the values of startDate and endDate in javascript? Also, why are you declaring the variables twice in JS? Also, using just [FromQuery] should be enough Commented Jul 23, 2018 at 14:16
  • What is the value of window.location.href in the JavaScript? Commented Jul 23, 2018 at 14:17
  • Updated with picures Commented Jul 23, 2018 at 14:22
  • The double decleration was a typo when i built the question here on stackowerflow... Commented Jul 23, 2018 at 14:23
  • I change the parameter names, so both JavaScript and C# the parameters are called "startDate" and "endDate" Commented Jul 23, 2018 at 14:24

2 Answers 2

3

From what you've posted it seems likely that windows.location.href is the problem and you are concatenating onto values which already exist in the URL. For example if your href was

http://www.somesite/Index?startDate=1&endate=2

Then your concatenated string would be:

http://www.somesite/Index?startDate=1&endate=2/Index?startDate=1&endDate=2

and your values would be:

startDate: 1
endDate: 2/Index/startDate=1

To solve the problem you could try using window.location.hostname instead of href.

Update

I think you should make the URL relative:

window.location = `/Index?startDate=${startDate}&endDate=${enddate}`;
Sign up to request clarification or add additional context in comments.

9 Comments

hmm..I see what you mean and it is probably the problem. How would you suggest i use the .hostname?
Where you have {window.location.href}/Index... replace it with {window.location.hostname}/Index...
To be super clear, you mean like this window.location.href = ${window.location.hostname}/Index/?startDate=${startDate}&endDate=${endDate};
Yes, except with window.location = at the beginning.
The code above gives this URL http://localhost:55817/localhost/Index/?startDate=2018-07-10&endDate=2018-07-29 tell me what you need, and Ill get it.
|
0

I solved it by removing the "index" in the url. The MVC framework automatically searches for the "index" ActionResult if the Action is missing in the url.

window.location = `Salaries?startDate=${startDate}&endDate=${endDate}`;

it also works like this:

window.location = `?startDate=${startDate}&endDate=${endDate}`;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.