Asp.net Core Razor Pages表单在Bootstrap模态框内不起作用。

huangapple go评论48阅读模式
英文:

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:

  1. 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>
  1. 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.

  2. Double-check the JavaScript code or event handlers in your application to ensure there are no client-side issues preventing the form submission.

  3. 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.

  4. Verify that there are no client-side validation or JavaScript code that could prevent the form submission.

  5. Ensure that the ModelState is valid in your PageModel class. If there are validation errors, it might prevent the form submission.

  6. 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:

&lt;!-- Modal --&gt;
&lt;form method=&quot;post&quot;&gt;
  &lt;div class=&quot;modal fade&quot; id=&quot;newBenchmark&quot; data-bs-backdrop=&quot;static&quot; data-bs-keyboard=&quot;false&quot;
  tabindex=&quot;-1&quot; aria-labelledby=&quot;staticBackdropLabel&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;div class=&quot;modal-dialog modal-dialog-scrollable modal-dialog-centered&quot;&gt;
      &lt;div class=&quot;modal-content&quot;&gt;
        &lt;div class=&quot;modal-header&quot;&gt;
          &lt;h5 class=&quot;modal-title&quot; id=&quot;staticBackdropLabel&quot;&gt;
            Start a new benchmark
          &lt;/h5&gt;
          &lt;button type=&quot;reset&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;
          &lt;/button&gt;
        &lt;/div&gt;
        &lt;div class=&quot;modal-body&quot;&gt;
          &lt;div class=&quot;form-floating mb-4&quot;&gt;
            &lt;input asp-for=&quot;CreateInput.Name&quot; class=&quot;form-control&quot; /&gt;
            &lt;label asp-for=&quot;CreateInput.Name&quot; class=&quot;form-label&quot;&gt;
              Name
            &lt;/label&gt;
            &lt;span asp-validation-for=&quot;CreateInput.Name&quot; class=&quot;text-danger&quot;&gt;
            &lt;/span&gt;
          &lt;/div&gt;
          &lt;div class=&quot;container&quot;&gt;
            &lt;table class=&quot;table table-striped align-middle&quot;&gt;
              &lt;caption class=&quot;caption-top&quot;&gt;
                Global
              &lt;/caption&gt;
              &lt;thead class=&quot;table-dark&quot;&gt;
                &lt;tr&gt;
                  &lt;th&gt;
                    &lt;div class=&quot;d-flex justify-content-center&quot;&gt;
                      Key
                    &lt;/div&gt;
                  &lt;/th&gt;
                  &lt;th&gt;
                    &lt;div class=&quot;d-flex justify-content-center&quot;&gt;
                      Value
                    &lt;/div&gt;
                  &lt;/th&gt;
                  &lt;th&gt;
                  &lt;/th&gt;
                &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                @foreach (var item in Model.ConfigurationItems) {
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;input readonly asp-for=&quot;@item.Item2&quot; class=&quot;form-control-plaintext&quot; /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;input readonly asp-for=&quot;@item.Item3&quot; class=&quot;form-control-plaintext&quot; /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                }
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;input asp-for=&quot;CreateInput.Key&quot; class=&quot;form-control&quot; /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;input asp-for=&quot;CreateInput.Value&quot; class=&quot;form-control&quot; /&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                    &lt;div class=&quot;d-flex justify-content-center&quot;&gt;
                      &lt;button type=&quot;button&quot; class=&quot;btn btn-primary m-1&quot; asp-page-handler=&quot;AddConfigItem&quot;&gt;
                        Add
                      &lt;/button&gt;
                    &lt;/div&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
              &lt;/tbody&gt;
            &lt;/table&gt;
          &lt;/div&gt;
          &lt;div class=&quot;modal-footer&quot;&gt;
            &lt;button type=&quot;reset&quot; class=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;
              Cancel
            &lt;/button&gt;
            &lt;button type=&quot;submit&quot; asp-page-handler=&quot;Start&quot; class=&quot;btn btn-primary&quot;&gt;
              Start
            &lt;/button&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/form&gt;

The handler method in my PageModel class:

public IActionResult OnPostAddConfigItem()
    {
        var scope = Scope.Global;
        if(CreateInput.Key == &quot;&quot; || CreateInput.Value == &quot;&quot;)
        {
            StatusMessage = &quot;Error: Key or value is empty.&quot;;
            
            return Page();
        }
        else
        {
            ConfigurationItems.Add((scope, CreateInput.Key, CreateInput.Value));
            CreateInput.Key = &quot;&quot;;
            CreateInput.Value = &quot;&quot;;
            
            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 -->

&lt;button type=&quot;button&quot; class=&quot;btn btn-primary m-1&quot; onclick=&quot;addConfig()&quot;&gt;Add&lt;/button&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年5月7日 09:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定