Disclaimer: I'm new to ASP.NET Core / Razor / MVC and am starting out on the 3.0 preview.
What I want to do is have a "button" on my page that adds a new empty item to a list so that the user can input some values. From what I've read (which is quite a bit) it sounds like having a hyperlink point towards a controller is the right approach to this. I cannot get it to actually work though. Here's my code:
Link pointing to controller / action:
<a class="btn btn-success" asp-controller="Customer" asp-action="AddProduct">New Product</a>
Controller:
public class CustomerController : Controller
{
public void AddProduct()
{
var tmp = "";
}
public string Index()
{
return "This is my default action...";
}
public string Welcome()
{
return "This is the Welcome action method...";
}
}
Startup.cs routing is default:
app.UseRouting(routes =>
{
routes.MapRazorPages();
});
With this setup, if I click the start button, I see the URL change to the below, but nothing else happens (no break point is hit, for example):
https://localhost:44358/Customers/Create?action=AddProduct&controller=Customer
I have tried to adding the route to specifically to the UseRouting code, like such:
app.UseRouting(routes =>
{
routes.MapRazorPages();
routes.MapControllerRoute(
name: "Customer",
template: "{controller=Customer}/{action=Welcome}");
});
But when I do this, it seems to break, as the text color changes (from white to black) and nothing happens when I click it.
Any idea on where I'm going wrong?
I do have one more question - which is how do you access the model data from the controller?
See middle of page here: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-core-3-0-preview-4/
In Startup.cs Configure:
app.UseRouting();
app.UseEndpoints(routes =>
{
routes.MapRazorPages();
routes.MapFallbackToPage("/Home");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With