© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
18 replies
M B V R K

How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?

Hello friends, I'm working on an
Asp.Net core 6
Asp.Net core 6
project, with
Areas
Areas
.
I have created a
PartialView
PartialView
, this partial view is created for
Search
Search
it contains an
Input
Input
element which will contain the value that the user wrote in, and after submit this form will send that value to an
Action
Action
called
Search
Search
in the controller.
I want this partial view to work generically, which I can use it with any
view
view
and during the submit the partial view should send that value to the
Action
Action
called
Search
Search
in the current
Controller
Controller
.

As you see the
Action
Action
will be fixed because it is known, but the
Controller
Controller
is unknown because it is dynamically changing.

An example of using this partial view:
  <div class="card card-preview">

        <div class="card-inner">

            @{
                await Html.RenderPartialAsync( "_SearchBox" );
            }
            
            <br>
// The rest of the view
  <div class="card card-preview">

        <div class="card-inner">

            @{
                await Html.RenderPartialAsync( "_SearchBox" );
            }
            
            <br>
// The rest of the view


How
Search
Search
action looks like in almost all controllers:

        [HttpGet("/[area]/[controller]/Search/{value}")]
        public async Task<IActionResult> Search(string value)
        {
            var response = await _mediator.Send( new SearchStudentAbsencesQuery( value ) );
            var model =  _mapper.Map<List<StudentAbsenceIndexVm>>( response.Entities );

            ViewBag.SearchValue = value;
   
            return View( model );
        }
        [HttpGet("/[area]/[controller]/Search/{value}")]
        public async Task<IActionResult> Search(string value)
        {
            var response = await _mediator.Send( new SearchStudentAbsencesQuery( value ) );
            var model =  _mapper.Map<List<StudentAbsenceIndexVm>>( response.Entities );

            ViewBag.SearchValue = value;
   
            return View( model );
        }


The problem:
When I write a value to search about and after submitted the form it goes to a false
Route
Route
and that route is not found
like this
https://localhost:44302/Controller/Search
https://localhost:44302/Controller/Search

As example let's assume that we are in a controller called
StudentAbsences
StudentAbsences
when I submit that search it goes to that false route
https://localhost:44302/StudentAbsence/Search
https://localhost:44302/StudentAbsence/Search
which need before the controller the
Area
Area
name.

My Question:
How I can make that
PartialView
PartialView
to automatically/dynamically know the
Controller
Controller
and the
Area
Area
?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Posting a list to a partial view in MVC
C#CC# / help
15mo ago
how to create a record in asp.net mvc based on something passed to the controller form a view.
C#CC# / help
3y ago
How can I change the image in this area in a form?
C#CC# / help
2y ago