点击提交按钮不点击剃须刀页面上的服务器端代码.net core 3.1



im正在处理一个.net core 3.1剃须刀页面项目,我有一个带有提交按钮的表单,但当我点击它时,执行不会影响页面模型中的Post处理程序。我在同一个剃须刀页面项目中也使用webApi

我的启动类

public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages()
.AddNewtonsoftJson()
.AddRazorRuntimeCompilation();
services.AddControllers()
.AddNewtonsoftJson()
.AddRazorRuntimeCompilation();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseDatabaseErrorPage();
//app.UseExceptionHandler("/Error");
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});

}

我的html

@page
@model CandidateBowl.WebPortal.Pages.JobAdvertisement.AddModel
@{
}
<form method="post" action="/Handlers?handler=Advertisement">
<div class="container-fluid">
<div class="row p-5 border pt-4 my-3 rounded">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="JobAdvertisementModel.CompanyId" />
<div class="col-12 px-3 mb-3 border-bottom text-center text-uppercase">
<h2 class="text-primary">Job Advertisement</h2>
</div>
<div class="col-9 pt-3">
<div class="input-group row mb-3">
<div class="col-md-3 col-sm-12 d-sm-block">
<label asp-for="JobAdvertisementModel.IndustryId"></label>
</div>
<div class="col-md-9 col-sm-12">
<select class="form-select" asp-for="JobAdvertisementModel.IndustryId" asp-items="Model.Options">
<option value=""></option>
</select>
<span class="text-danger" asp-validation-for="JobAdvertisementModel.IndustryId"></span>
</div>
</div>
<div class="input-group row mb-3">
<div class="col-md-3 col-sm-12 d-sm-block">
<label asp-for="JobAdvertisementModel.Title"></label>
</div>
<div class="col-md-9 col-sm-12">
<input class="form-control" asp-for="JobAdvertisementModel.Title" />
<span class="text-danger" asp-validation-for="JobAdvertisementModel.Title"></span>
</div>
</div>
<div class="input-group row mb-3">
<div class="col-md-3 col-sm-12 d-sm-block">
<label asp-for="JobAdvertisementModel.Salary"></label>
</div>
<div class="col-md-9 col-sm-12">
@* use year selector *@
<input class="form-control" asp-for="JobAdvertisementModel.Salary" />
<span class="text-danger" asp-validation-for="JobAdvertisementModel.Salary"></span>
</div>
</div>
<div class="input-group row mb-3">
<div class="col-md-3 col-sm-12 d-sm-block">
<label asp-for="JobAdvertisementModel.Location"></label>
</div>
<div class="col-md-9 col-sm-12">
@* use year selector *@
<input class="form-control" asp-for="JobAdvertisementModel.Location" />
<span class="text-danger" asp-validation-for="JobAdvertisementModel.Location"></span>
</div>
</div>
<div class="input-group row mb-3">
<div class="col-md-3 col-sm-12 d-sm-block">
<label asp-for="JobAdvertisementModel.Role"></label>
</div>
<div class="col-md-9 col-sm-12">
@* use year selector *@
<input class="form-control" asp-for="JobAdvertisementModel.Role" />
<span class="text-danger" asp-validation-for="JobAdvertisementModel.Role"></span>
</div>
</div>
<div class="input-group row mb-3">
<div class="col-md-3 col-sm-12 d-sm-block">
<label asp-for="JobAdvertisementModel.Requirements"></label>
</div>
<div class="col-md-9 col-sm-12">
@* use year selector *@
<textarea class="form-control" asp-for="JobAdvertisementModel.Requirements"></textarea>
<span class="text-danger" asp-validation-for="JobAdvertisementModel.Requirements"></span>
</div>
</div>

<div class="input-group row mb-3">
<div class="col-md-9 offset-3 col-sm-12">
<div class="row">
<div class="col-md-6 col-sm-12">
<button class="btn btn-primary form-control">Save</button>
</div>
<div class="col-md-6 col-sm-12">
@* return to candidate details page *@
<a asp-page="Index" class="btn btn-success form-control">Back To Job Advertisements </a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
@section Scripts{
<script src="~/js/tinMCE.js" type="text/javascript"></script>
}

后面的我的代码

public class AddModel : PageModel
{
private readonly IJobAdvertisementRepository jobAdvertisementRepository;
private readonly IIndustryRepository industryRepository;
public AddModel(IJobAdvertisementRepository jobAdvertisementRepository, IIndustryRepository industryRepository)
{
this.jobAdvertisementRepository = jobAdvertisementRepository;
this.industryRepository = industryRepository;
}
[BindProperty]
public JobAdvertisementModel JobAdvertisementModel { get; set; }
public List<SelectListItem> Options { get; set; }
public async Task<IActionResult> OnGetAsync()
{
Options = industryRepository.GetIndustries().Result.Select(a => new SelectListItem
{
Value = a.Id.ToString(),
Text = a.Name
}).ToList();
return Page();
}
public async Task<IActionResult> OnPostAdvertisementAsync()
{
if (!ModelState.IsValid)
{
Options = industryRepository.GetIndustries().Result.Select(a => new SelectListItem
{
Value = a.Id.ToString(),
Text = a.Name
}).ToList();
return Page();
}
var id = await jobAdvertisementRepository.AddAdvertisement(JobAdvertisementModel);
if (id == 0)
return Page();
return RedirectToPage();//still to decide where it should go
}
}

问题是什么,将非常感谢您的帮助

您发布了You have a from that has a submit button,我想您的意思是这行<button class="btn btn-primary form-control">Save</button>不起作用。

试试这个

<input type="submit" value="Save" class="btn btn-primary form-control"/>

还要确保action="/Handlers?handler=Advertisement"在您的表格中是正确的。

请更改

<form method="post" action="/Handlers?handler=Advertisement">

<form method="post" asp-page-handler="Advertisement">

这将生成以下HTML:

<form method="post" action="/Add?handler=Advertisement">

就像@PritomSarkar所说的,修复你的按钮

<input type="submit" value="Save" class="btn btn-primary/>

相关内容

最新更新