I am building a web application for a parser. The home page is a simple razor view with basic controls, that will allow the user to add the input, and later will show the results of the parse. One of the components of this main page is a ViewComponent that returns one of the two relevant partial views for the parser's functionality: a first view that allows the user to add the input, and a result view showing the output. The decision of showing one or the other view will depend on a bool variable, "parsed", that will register whether the user has added input and parsed or not. We'll call the main page "Home", the input view "Main" and the output view "Results". If the user adds input and clicks the "Parse" button, "parsed" will be true and the Results view will load.
I have configured the logic and routes for this behaviour, and I can test it without a problem if I manually type the routes, adding the optional parameter. So, for example, all of these work properly:
- localhost:1111/ -> Shows Main.
- localhost:1111/?parsed=false -> Shows Main.
- localhost:1111/?parsed=true -> Shows Result.
- localhost:1111/parser/?parsed=false -> Shows Main, so on and so forth.
What's the problem then? When I hit the "Parse" button, I can't make the system retrieve the expected url localhost:1111/?parsed=true, obtaining instead localhost:1111/?. From this tutorial (and many more I've read) I understand that I need to use asp-route-[parameter], and that it has to match the signature of the method that is being called in asp-action, but I can't make it work.
This is the button in question:
<div class="col-2">
<form>
<button class="button"
asp-controller="Home" asp-action="Index"
asp-route-parsed="true">
Start Parsing
</button>
</form>
</div>
And this is the method it calls:
[Route("parser/{parsed:bool?}")]
[Route("/{parsed:bool?}")]
[Route("")]
public ViewResult Index(bool parsed = false) {
ViewData["Title"] = "Home";
if (parsed == true) {
Helper.parsed = true;
} else {
Helper.parsed = false;
}
return View("Index", Helper);
}
I'll really appreciate some orientation here, I've read several articles and the documentation, but can't see where the problem is. In anyone would like to take a look at the whole system, the code is available here. Thanks in advance.