TEC C
TEC C

Reputation: 153

Button not executing correct method in controller class

I am new to .NET core and am creating my first small program. in the application I click a button and it should execute a method that performs an action. According to what I've found online it should be fairly simple to do this. Unfortunately I seemed to be missing something in my research. The button keeps calling the wrong method. There must be a setting some ware that I am missing. the way I understand it all I need to do is after asp-controller put the controller I want to use and after asp-action put the Method. Unfortunately it always calls the method called at startup.

<form method="post">
    <button asp-controller="Brochure" asp-action="Recall">Click Me</button>

</form>

Method called:

  public class HomeController : Controller
    {        
        public ViewResult BrochureMain()
        {

        int ao = 0;
        int po = 0;

        using (var ctx = new BrochureDBContext())
        {
            var brochureSet = ctx.Brochure.FromSql  ($"usp_OAK_Brochure_Count")
                                            .ToList();
            foreach (var agencyOrder in brochureSet)
            {
                ao = agencyOrder.AgencyOrders;
                po = agencyOrder.PaxOrders;
            }
        }
        var model = new Brochure { AgencyOrders = ao, PaxOrders = po };
        return View(model);
    }

The method I want to call:

 public class BrochureController : Controller
{


    [HttpPost]

    public String Recall()
    {
        //Product product = repository.Products
        //.FirstOrDefault(p => p.ProductID == productId);
        //if (product != null)
        //{
        //    cart.RemoveLine(product);
        //}
        return "Recall Test";
        //return View("ReloadOrders");
    }

Startup:

 public class Startup
{
    public IConfiguration Configuration { get; set; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton<IMessageService, ConfigurationMessageService>();
        services.AddSingleton(provider => Configuration);
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessageService msg)
    {           
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=BrochureMain}/{id?}");
        });
        app.Run(async (context) =>
        {               
            await context.Response.WriteAsync(msg.GetMessage());
        });
    }
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");
        if (env.IsDevelopment())

        Configuration = builder.Build();
    }       
}

Upvotes: 0

Views: 112

Answers (2)

Meysam Gheysaryan
Meysam Gheysaryan

Reputation: 55

The asp-controller/asp-action attributes go on the form, and use button as submit type.

<form method="post" asp-controller="Brochure" asp-action="Recall">
<button type="submit" >Click Me</button>

and for view response of recall action in browser, you should remove

 app.Run(async (context) =>
    {               
        await context.Response.WriteAsync(msg.GetMessage());
    });

from startup class.

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239250

The asp-controller/asp-action attributes go on the form, not the button.

Upvotes: 1

Related Questions