英文:
Asp.net Core Razor Pages form inside Bootstrap modal not working
问题
The code you provided seems to be related to a web application using Razor Pages in C#. It appears you're encountering an issue where the "Add" button in your modal is not triggering the OnPostAddConfigItem
handler method as expected, and you're not seeing any POST requests in the network.
I suggest checking the following:
- Ensure that your form element has the correct
asp-page-handler
attribute set to "AddConfigItem" on the "Add" button:
<button type="button" class="btn btn-primary m-1" asp-page-handler="AddConfigItem">
Add
</button>
-
Verify that the
OnPostAddConfigItem
method is in the correct PageModel class and that there are no typos or mismatches in the method name or parameters. -
Double-check the JavaScript code or event handlers in your application to ensure there are no client-side issues preventing the form submission.
-
Check if there are any JavaScript errors or console messages in your browser's developer console that might provide clues about what's going wrong.
-
Verify that there are no client-side validation or JavaScript code that could prevent the form submission.
-
Ensure that the
ModelState
is valid in your PageModel class. If there are validation errors, it might prevent the form submission. -
If you're using any client-side libraries or frameworks, make sure they are not interfering with the form submission process.
By carefully reviewing these aspects, you should be able to identify the reason why the "Add" button is not triggering the OnPostAddConfigItem
handler method.
英文:
I would like to ask for your help, I have this modal, which properly pops up and displays the form fields, even the validation is great and the submit button also works, but the Add button doesn't and I couldn't figure out the reason why.
The code of the modal:
<!-- Modal -->
<form method="post">
<div class="modal fade" id="newBenchmark" data-bs-backdrop="static" data-bs-keyboard="false"
tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-scrollable modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">
Start a new benchmark
</h5>
<button type="reset" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
</button>
</div>
<div class="modal-body">
<div class="form-floating mb-4">
<input asp-for="CreateInput.Name" class="form-control" />
<label asp-for="CreateInput.Name" class="form-label">
Name
</label>
<span asp-validation-for="CreateInput.Name" class="text-danger">
</span>
</div>
<div class="container">
<table class="table table-striped align-middle">
<caption class="caption-top">
Global
</caption>
<thead class="table-dark">
<tr>
<th>
<div class="d-flex justify-content-center">
Key
</div>
</th>
<th>
<div class="d-flex justify-content-center">
Value
</div>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.ConfigurationItems) {
<tr>
<td>
<input readonly asp-for="@item.Item2" class="form-control-plaintext" />
</td>
<td>
<input readonly asp-for="@item.Item3" class="form-control-plaintext" />
</td>
<td>
</td>
</tr>
}
<tr>
<td>
<input asp-for="CreateInput.Key" class="form-control" />
</td>
<td>
<input asp-for="CreateInput.Value" class="form-control" />
</td>
<td>
<div class="d-flex justify-content-center">
<button type="button" class="btn btn-primary m-1" asp-page-handler="AddConfigItem">
Add
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="reset" class="btn btn-secondary" data-bs-dismiss="modal">
Cancel
</button>
<button type="submit" asp-page-handler="Start" class="btn btn-primary">
Start
</button>
</div>
</div>
</div>
</div>
</div>
</form>
The handler method in my PageModel class:
public IActionResult OnPostAddConfigItem()
{
var scope = Scope.Global;
if(CreateInput.Key == "" || CreateInput.Value == "")
{
StatusMessage = "Error: Key or value is empty.";
return Page();
}
else
{
ConfigurationItems.Add((scope, CreateInput.Key, CreateInput.Value));
CreateInput.Key = "";
CreateInput.Value = "";
return Page();
}
}
I ran the program in debugging mode, to see, whether it gets into the handler method, but it didn't.
Than I stalked it a bit with the browser dev tools and I saw, that there weren't any POST requests on the network by clicking on the button.
I tried to find similar problems on the net, but I didn't succeed.
答案1
得分: 0
"如Mike Brind所说,type="button"的按钮默认不执行任何操作,其功能是可以被按下。我使用它,因为我不想提交和验证整个表单的数据,而是在我的表单内部有一个表格,我使用这个按钮动态添加数据到这个表格,而不需要重新加载或提交表单。
我可以通过将JS函数订阅到这个按钮的onclick事件来实现这一点,如下所示:
<button type=\"button\" class=\"btn btn-primary m-1\" onclick=\"addConfig()\">添加</button>
然后在JS函数中,例如addConfig()
中,我实现了一个ajax POST请求调用,并调用了我最初想要通过asp-page-handler
标签调用的处理程序函数。
如果你想要在不提交或重新加载表单的情况下执行一些任务,我可以建议这种方式。
英文:
As Mike Brind said, a button with a type="button" does nothing by default and its function is that it can be pressed. I used it, because I didn't want to submit and validate the data of the whole form, rather I had a table inside my form and I used this button to add data to this table dynamically, without reloading or submitting the form.
I could manage this, via subscribing a JS function to this button's onclick event, by writing this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// in a site.js file or embedded in html:
function addConfig(){
/* do something here */
}
<!-- language: lang-html -->
<button type="button" class="btn btn-primary m-1" onclick="addConfig()">Add</button>
<!-- end snippet -->
And after that in the JS function, in this example: addConfig()
, I implemented an ajax POST request call and called the handler function, that I originally wanted to call by the asp-page-handler
tag.
I can suggest this way, if you wanna do some job from a form, but without submitting or reloading the the form.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论