Is it possible to use a Javascript onchange function to replace "-" with "/" in a URL string

I am working on an MVC3 web page where a URL parameter is used to represent a date in the format dd-MM-yyyy.

public ActionResult Calendar(string startDate, string UserId, string status, bool myPage = false)

Within the calendar view, there is a partial view that triggers an onchange event whenever a dropdown list value is changed. This event corresponds to the status parameter in the method and is also passed in the URL.

The route mapping for the calendar view is as follows:

routes.MapRoute(
            "TimesheetCalendar", // Route name
            "Timesheet/Calendar/{startDate}/{UserId}", // URL with parameters
            new { controller = "Timesheet", action = "Calendar", UserId = UrlParameter.Optional } // Parameter defaults
        );

Included within the partial view is a JavaScript onchange event like this:

<script type="text/javascript>
$(function () {
    $(".ddl").change(function () { changePage(); });
});

function changePage() {
    location.href = '@Url.Action("Calendar", "Controller", new { startDate = @ViewBag.startDate, UserId = (string)null })/' + '@ViewBag.userId' + '?status=' + $('#ControllerStatus').val() + '&<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="573a2e073630326a67d251b21414c7b271d1b">[email protected]</a>';
}

In the Calendar GET method of my controller, I set the ViewBag.startdate as date.ToString(dd-MM-yyyy).

Everything works fine most of the time. However, sometimes when changing the dropdown list status, the page refreshes but encounters a 404 error. It seems that whenever the week shown on the calendar is the current week, the URL changes the hyphens in the date representation from dd-MM-yyyy to dd/MM/yyyy, causing issues.

As a newcomer to JavaScript, I'm unsure why this happens. Any insights or advice would be greatly appreciated!

Answer №1

Apologies for the oversight. I recently discovered that the default ToString method for dates includes a /. It seems that while transferring dates between the main view and its partial view, I mistakenly used ToString() instead of ToString("dd-MM-yyyy")

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method to retrieve data in Vue when utilizing arrow functions?

When utilizing Vue and arrow functions in methods, we encounter the issue that arrow functions do not have a this keyword. The question then arises of how to access data properties such as list or object. export default { data() { return { list ...

Ways to have a dropdown menu automatically close when opening another dropdown

Having an issue with managing multiple href links on a single page. When one link is clicked, it opens a dropdown, but if another link is clicked without closing the first one, the first dropdown remains open. How can I resolve this? I initially attempted ...

Tips for directing specific mouse interactions to the underlying elements within an SVG

I'm working with a scatterplot that includes a brush layer for zooming in on the data and voronois for mouse hover snapping. However, I've encountered an issue where the click event doesn't fall through to the brush layer when the voronoi pa ...

Refresh Chart Information using Ng2-Charts in Charts.js

Utilizing chart.js and ng2-charts, I am developing gauge charts for my platform to monitor the fluid levels inside a machine's tank. The values are retrieved from an external API, but I am encountering an issue where the charts are rendered before I ...

Typescript encountered an error indicating that the property "x" is nonexistent on the specified type of 'Readonly<Props> & Readonly<{ children?: ReactNode; }>'

I recently started using Typescript with Nextjs and encountered an error while trying to typecheck a basic component. How can I resolve the error and typecheck my array of objects? ERROR in C:/Users/Matt/sites/shell/pages/index.tsx(22,4): 22:4 Property &a ...

Webpack's equivalent to JSPM's override functionality

I am currently working on a project where I have specified an override for a package inside the jspm block in the package.json file. The content of the override block is as follows: "overrides": { "github:repoName/myPackage@master": { "main": "js/so ...

Is it possible for me to overlap a text over hidden text, and if the hidden text becomes visible through JavaScript, my text will shift to the right of the now visible hidden text?

I'm currently working on a website for a company that requires users to complete all the information before proceeding. To achieve this, I created a form with the following code: <form action="Owners Infoback.php" onsubmit="return validateFo ...

Organizing React State Arrays

I am struggling to organize an array of objects stored in a React hook state. After sorting, this array is passed to a FlatList component. const { notifications } = useContext(MainContext); const [sortedNotifications, setSortedNotifications] = useState([]) ...

Endlessly triggering document.execCommand, the JavaScript selectionchange-EventListener seems to have a mind of

I recently implemented an event listener for selectionchange in the following manner: document.addEventListener("selectionchange", function() { highlight(); console.log("selectionchange-triggered"); }, false); After that, I included the code bel ...

Revolutionizing messaging with Vue JS and Firebase

My web application is designed to check if a user has messages in the Firebase database. If messages are found, it retrieves the data from the users who sent those messages from my local database and displays them in a list using a v-for loop. The display ...

Angular Cascade of Multiple Requests

I am currently working on a project where I need to develop a service that can take CSV data, convert it into rows, and then make API requests for each row, adding the formatted results to an output string. To explain further, my code (written in Coffeesc ...

Unable to adjust item inventory in MongoDB, utilizing Node.js and React.js

I'm currently developing a warehouse management website where I store my data in MongoDB. One of the key features I am working on is the ability to update product quantity with just a button click and have it reflected on the user interface. However, ...

Guide to setting up page.js

I am eager to incorporate page.js into my project. However, it seems like it requires some type of build system, so I'm not able to locate a single page.js file to include in my page and immediately begin using. page('/',index) Does anyone ...

Updating information with AJAX upon clicking a hyperlink

I am trying to implement AJAX for replacing the contents within a div. The setup of the application is quite complex, but I have simplified it to focus on getting the basic concept to work first. Currently, my goal is simply to replace a div based on the ...

I'm trying to implement a command in my bot that displays the number of Discord members, but I keep encountering an error

I've encountered an issue with the discord.js v13 member count command. Despite seeking help in various Discord servers, I haven't received any assistance. If anyone could offer their expertise, it would be greatly appreciated. const Discord = re ...

Discovering the country associated with a country code using ngx-intl-tel-input

In my application, I am trying to implement a phone number field using this StackBlitz link. However, I have observed that it is not possible to search for a country by typing the country code (e.g., +231) in the country search dropdown. When I type a coun ...

Highcharts: Limiting the yAxis values to a maximum of two decimal places

Here is the code for my graph, which retrieves data from a PHP page and adds some series: $('#grafico_1').highcharts({ chart: { type: 'line', zoomType: 'xy', animation : false, events: { selection: functi ...

Error encountered due to incompatible date format in SimpleDateFormat

How can I extract milliseconds from a date without encountering an exception? I'm receiving the error: java.text.ParseException: Unparseable date: "Thu Jul 25 10:56:29 GMT+02:00 2019" This is the approach I've taken: Firstly, I retrieved the ...

explore the route with the help of jquery scrolling feature

Is there a way to implement scrolling functionality for tab headers similar to this demo? I have a list of elements within a div that I need to scroll like the tabs in the demo. I attempted to achieve this by setting the position of the inner elements to ...

Ways to transform the DropBox chooser button into an image

I'm looking to incorporate an image in place of Dropbox's default chooser button. Despite reviewing their API documentation, I haven't come across any methods to use alternative elements for the Dropbox chooser. Does anyone have a solution f ...