C
C#2y ago
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 project, with Areas. I have created a PartialView, this partial view is created for Search it contains an Input element which will contain the value that the user wrote in, and after submit this form will send that value to an Action called Search in the controller. I want this partial view to work generically, which I can use it with any view and during the submit the partial view should send that value to the Action called Search in the current Controller. As you see the Action will be fixed because it is known, but the 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 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 Routeand that route is not found like this https://localhost:44302/Controller/Search As example let's assume that we are in a controller called StudentAbsences when I submit that search it goes to that false route https://localhost:44302/StudentAbsence/Search which need before the controller the Area name. My Question: How I can make that PartialView to automatically/dynamically know the Controller and the Area ?
12 Replies
M B V R K
M B V R K2y ago
The Partial view:
<form asp-action="Search">

<div class="form-group">
<div class="form-control-wrap">
<div class="form-icon form-icon-right">
<em class="icon ni ni-search"></em>
</div>

@if ( ViewBag.SearchValue != null )
{
<input type="text" class="form-control form-control-xl form-control-outlined" id="value" value="@ViewBag.SearchValue">
}
else
{
<input type="text" class="form-control form-control-xl form-control-outlined" id="value">
}

<label class="form-label-outlined" for="outlined-right-icon">Rechercher</label>
</div>
</div>

</form>
<form asp-action="Search">

<div class="form-group">
<div class="form-control-wrap">
<div class="form-icon form-icon-right">
<em class="icon ni ni-search"></em>
</div>

@if ( ViewBag.SearchValue != null )
{
<input type="text" class="form-control form-control-xl form-control-outlined" id="value" value="@ViewBag.SearchValue">
}
else
{
<input type="text" class="form-control form-control-xl form-control-outlined" id="value">
}

<label class="form-label-outlined" for="outlined-right-icon">Rechercher</label>
</div>
</div>

</form>
Nickolaki
Nickolaki2y ago
You could make it so that on the submit of the search form it sends a string values of the area/controller it needs to go to. These could be hidden inputs.
M B V R K
M B V R K2y ago
??????????
Nickolaki
Nickolaki2y ago
Lemme re-write the action, on my phone so bare with
M B V R K
M B V R K2y ago
ok, thanks in advance
Nickolaki
Nickolaki2y ago
Think you can use: return RedirectToRoute(new { area= “”, controller = "", action = "", model = model }); Rather than the form only submit the search query. Have it post a model that contains the search query, area and controller name.
M B V R K
M B V R K2y ago
where should I put this piece of code?
Nickolaki
Nickolaki2y ago
Replace your: return View(model) With the return redirect to view code public class SearchViewModel { public string Query { get; set; } public string Controller { get; set; } public string Area { get; set; } } That can be your model
M B V R K
M B V R K2y ago
but the issue is not in the Controller, the issue is in the _SearchBox PartialView
Nickolaki
Nickolaki2y ago
Have you got the area attribute named on each of the controllers?
M B V R K
M B V R K2y ago
yeah
[Area( nameof(AppUserRole.Admin))] //<<<<< Admin
[Route( "[area]/[controller]/[action]")]
[Authorize(Roles = nameof(AppUserRole.Admin))]
public class StudentAbsenceController : Controller
{
// Logic here
}
[Area( nameof(AppUserRole.Admin))] //<<<<< Admin
[Route( "[area]/[controller]/[action]")]
[Authorize(Roles = nameof(AppUserRole.Admin))]
public class StudentAbsenceController : Controller
{
// Logic here
}
Nickolaki
Nickolaki2y ago
Then I think you need to rename the http on the a search action to this:
[HttpGet("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("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 );
}
Remove the area and controller bit on the http attribute as I’m pretty sure that’s what’s causing the confusion