我是.NET核心的新手,正在创建我的第一个小程序。在应用程序中,我单击一个按钮,它应该执行一个执行操作的方法。根据我在网上的发现,这样做应该是相当简单的。不幸的是,我似乎在研究中遗漏了一些东西。按钮总是调用错误的方法。一定有一些我遗漏的布景。我理解它的方式是,我需要做的就是在asp-controller之后放入我想要使用的控制器,在asp-action之后放入方法。不幸的是,它总是在启动时调用调用的方法。
<form method="post">
<button asp-controller="Brochure" asp-action="Recall">Click Me</button>
</form>
调用的方法:
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);
}
我想调用的方法:
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");
}
启动:
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();
}
}
发布于 2019-09-27 18:08:02
asp-controller
/asp-action
属性位于表单上,而不是按钮上。
发布于 2019-09-28 14:10:15
asp-controller/asp-action属性放在表单上,并使用button作为提交类型。
<form method="post" asp-controller="Brochure" asp-action="Recall">
<button type="submit" >Click Me</button>
对于在浏览器中查看召回操作的响应,您应该删除
app.Run(async (context) =>
{
await context.Response.WriteAsync(msg.GetMessage());
});
来自启动课。
https://stackoverflow.com/questions/58138605
复制