Is it possible to retrieve the controller path for an AJAX request from within a partial view?

Looking for a solution to fully decouple and reuse a partial view that allows users to select dates and filter results based on those dates. This widget can be used on multiple pages, so I wanted to add event listeners that would submit the form within the date partial view.

In my main view:

@{using (Html.BeginForm("getActivityManagementTwoActivities", "CallCenter", FormMethod.Get, new { id = "ActivityTwoForm" }))
    {
        <div class="row ">
            <div class="col-md-12" style="float:right;">
                <div class="dateWrapper">@{ Html.RenderPartial("_DateFilter"); } </div>
            </div> 
            //... 
       @{ Html.RenderPartial("_ActivityManagementCollapseComponent", Model);
}

My controllers:

    public async Task<PartialViewResult> getActivityManagementTwoActivities(DateFilterModel filters, int results = 25)
    {
        var model = await GetFullAndPartialViewModel(filters, results);
        return PartialView("_ActivityManagementCollapseComponent", model);
    }

Here is the code for the date partial view:

<div class="date-text-collapsible">
<form method="get">
    <table>
        <tr>
            <td style="text-align:right; vertical-align: top; width: 160px">
                Time filter:
            </td>
            <td style="display:inline-block; width:150px">
                <input id="radio1" name="radioToggle" type="radio"> Recent
                <select id="timeFilter" name="timeFilter" class="hokage">
                    <option value="Last 30 days">at least 18</option>
                </select>
                <br />
                <input id="radio2" name="radioToggle" class="radioTimeFilter" type="radio"> Date Range
                <div id="dateContent" class="radio-content" style="display:none;">
                    <input id="txtDateOneFilter" type="text" style="width: 80px;" placeholder="Start Date" name="dateStart" class="hokage" />&nbsp;to&nbsp;
                    <input id="txtDateTwoFilter" type="text" style="width: 80px;" placeholder="End Date" name="dateEnd" class="hokage" />
                </div>
                <br />
            </td>
        </tr>    
    </table>
</form>

Attempted to add event listeners like this:

    var inputDateBoxes = document.getElementsByClassName("hokage");
    addOnChangeListenersToSubmit(inputDateBoxes);
    function addOnChangeListenersToSubmit(arrayOfElements) {
      for (var i = 0; i < arrayOfElements.length; i++) {
        arrayOfElements[i].addEventListener('change', function () {
          this.form.submit();
        })
      }
    }

However, this approach only returns the partial view by itself. It seems like I may need to use AJAX or another method. How can I update the partial view from within the date filter partial?

Answer №1

If you're looking to retrieve the controller name and action name in razor syntax, there are a few methods available:

string controllerName = this.ViewContext.RouteData.Values["controller"].ToString();        
string actionName = this.ViewContext.RouteData.Values["action"].ToString();

This information can serve as a foundation for further development.

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

Learn the steps for showing a kendoDropDownList using ajax in the ASP.NET MVC framework

I am currently facing an issue with my Kendo kendoDropDownList. I am trying to populate the drop down values by making an Ajax call. However, every time I click on the drop down, it calls the Action and retrieves the values, but the dropdown does not displ ...

Troubleshooting: Issue with Displaying $Http JSON Response in AngularJS View

Struggling to retrieve JSON data from an API and display it in a view using AngularJS. Although I am able to fetch the data correctly, I am facing difficulties in showing it in the view. Whenever I try to access the object's data, I constantly receive ...

"Enhancing user experience with Ajax by dynamically loading diverse content via

My goal is to change the URL in the address bar and add a class to the anchor tag when clicking on a tab. I came across a great example on jsfiddle that someone shared on stackoverflow. http://jsfiddle.net/VcQKr/2/ The issue I'm facing is that while ...

Close the parent electron window while keeping the child window open

I am currently working on a project where I need to create an electron app that displays a splash screen initially and then opens a new window before closing the splash screen. However, despite my efforts, I am facing challenges in achieving this functio ...

Error: Unable to locate module adaptivecards-templating

After adding the module through the command: npm install adaptive-expressions adaptivecards-templating --save and importing it, I encountered an error when trying to run my application: ...

Encountering difficulty selecting a dropdown sub-menu using Selenium WebDriver

I'm currently working on automating a website with selenium webdriver. The issue I'm encountering is that when I try to click on a menu item, the submenu pops up (actually a misplaced dropdown, UI issue), and although I can locate the element of ...

What are the steps to add 8 columns to the second row in my table?

this is an example of HTML code that showcases a table structure with multiple rows and columns. You can view the full code snippet here. The table includes different sections, such as section1, section2, section3, and section4 in the first row (tr). In t ...

Is it possible to change the inner html to null during an active ajax request?

My goal is to have two separate data.html files inserted into two different HTML files without needing a click event. I want the structure of my data.html files to remain consistent, while allowing the template of my website to change dynamically by callin ...

Updating a model in Angular JS after sending a JSON GET request: A step-by-step guide

Having issues with my simple page that displays records based on the selected year. The sidebar selection is not highlighting properly. The initial data loads fine when I first visit the page. However, when I try to change the year and click "Go", the mai ...

Mobile Image Gallery by Adobe Edge

My current project involves using Adobe Edge Animate for the majority of my website, but I am looking to create a mobile version as well. In order to achieve this, I need to transition from onClick events to onTouch events. However, I am struggling to find ...

Using an object does not result in updated data, while data changes are typically observed when using a variable

In the process of developing a function to update a custom checkbox upon clicking (without resorting to using the native checkbox for specific reasons). Here's the code snippet for the checkbox: <div class="tick-box" :class="{ tick: isTicked }" @ ...

Updating various div elements using AJAX based on validation errors

Seeking guidance on submitting a form via AJAX with prototype and the built-in rails 'form_remote_tag' helper. I want to update one div (a status area) for form validation errors, and another div (where the form is located) if submission is suc ...

chart for visualizing two rows with matching IDs and dates

I have been attempting to accomplish the following two scenarios: 1) When both ID and dates are the same in the chart, but the descriptions differ, I want to display them on separate lines. 2) If there is not enough room to show the row label, I would li ...

Customizing the CSS of the TinyMCE editor within a React application

Incorporating TinyMCE 5 into my React project has been an interesting challenge. I'm looking to personalize the editor by adjusting elements like borders and adding box shadows to the toolbar. Despite attempting to add CSS through the content_css prop ...

Session is required for req.flash() function in node.js to work properly

I recently started working with Node.js and I'm encountering an issue with sessions. I developed a simple application and tried to run it locally, but ran into some errors. Below are the details of my code along with the error messages: BAPS.js (app. ...

The ItemsController is throwing an error because it cannot find a constant for Item

Upon attempting to access the localhost:3000/items/new link, an uninitialized constant error in ItemsController::Item is encountered. The source of this issue is unclear at the moment. The structure of my Application.html.erb layout is as follows: <!D ...

socket.io establishes several sockets for each connection

At times, when the server is experiencing some load, connecting to the page may result in multiple sockets being created. If there is significant lag, the connection may never be established while additional sockets are generated every second indefinitely. ...

What is the procedure for passing arguments to Google signIn in a NextJS backend?

I am currently working on implementing a role-based Google sign-in feature in a Next.js 13 app using next-auth. This setup involves calling two different tables to create users based on their assigned roles. For the database, I am utilizing mongodb/mongoo ...

Base64 versus UTF-8 charset: What sets them apart?

While working with binary files, I discovered that: If it's a text-based file, I need to use data:text/plain;charset=utf-8, and if it's a multimedia file, I should use data:image/png;base64, I attempted to use base64 encoding and encountered a ...

Tips for accessing basic information from these websites without encountering CORS issues

Having issues retrieving data from the following two sites: and Eurolottery. The CORS issue is causing the problem, and I was able to retrieve data using a Chrome CORS extension and the provided code below: var HttpClient = function() { this.get = fu ...