MVC - 似乎无法使用 AJAX 帮助程序发布到控制器操作

发布于 2024-12-09 10:03:35 字数 4724 浏览 0 评论 0原文

我在使用 AJAX 帮助程序将模型发布到控制器操作时遇到问题。控制器的动作根本不会受到影响。

代码:

视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ServiceNumberModel>" %>

<div id="ServiceNumberPanel">
    <h1>
        Based on your location we offer different plans</h1>
    <% using (Ajax.BeginForm("AvailablePlans", "ServiceCheck", new AjaxOptions { UpdateTargetId = "AdslPlansPanel", HttpMethod = "Post" } ))
       { %>
            <%=Html.ValidationSummary(true)%>
            Your Phone Number:
            <%=Html.TextBoxFor(m => m.HomePhoneNumber)%><br />
            <%=Html.CheckBoxFor(m => m.BundleWithHomePhone)%>Bundle with Home Phone? <br/>
            <input id="CheckServiceAvailabilityButton" type="submit" value="Check"/>
    <% } %>
</div>

模型:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace ServiceCheck
{
    public class ServiceNumberModel
    {
        [Required(ErrorMessage = "Please enter your phone number.")]
        [RegularExpression(@"^[0]\d{9}$", ErrorMessage = "Please enter your phone number, including the area code")]
        [DisplayName("Phone Number:")]
        public string HomePhoneNumber { get; set; }


        public bool BundleWithHomePhone { get; set; }
    }
}

控制器:

 [HttpPost]
        public ActionResult AvailablePlans(ServiceNumberModel serviceNumberModel)
        {
            if (serviceNumberModel.BundleWithHomePhone)
                return ReturnAvailableBundledPlans(serviceNumberModel);

            return View("Index");
        }

生成的 HTML:< /strong>

<div id="ServiceNumberPanel">
    <h1>
        Based on your location we offer different plans</h1>
    <form action="/ServiceCheck/AvailablePlans" data-ajax="true" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-update="#AdslPlansPanel" id="form0" method="post">
            Your Phone Number:
            <input data-val="true" data-val-regex="Please enter your phone number, including the area code" data-val-regex-pattern="^[0]\d{9}$" data-val-required="Please enter your phone number." id="HomePhoneNumber" name="HomePhoneNumber" type="text" value="" /><br />
            <input data-val="true" data-val-required="The BundleWithHomePhone field is required." id="BundleWithHomePhone" name="BundleWithHomePhone" type="checkbox" value="true" /><input name="BundleWithHomePhone" type="hidden" value="false" />Bundle with Home Phone? <br/>

            <input id="CheckServiceAvailabilityButton" type="submit" value="Check"/>
    </form>
</div>

行为: 当我单击提交时,出现“找不到资源错误”:

找不到资源。描述:HTTP 404。您的资源 正在寻找(或其依赖项之一)可能已被删除, 已更名,或暂时不可用。请查看 以下 URL 并确保其拼写正确。

请求的 URL:/ServiceCheck

有趣的是,当我使用 firebug 查看源代码时,我看不到表单标签:

<div class="ExistingContent">
<h1>ServiceCheck</h1>
<div id="ServiceNumberPanel">
<div id="ServiceNumberPanel">
<h1> Based on your location we offer different plans</h1>
Your Phone Number:
<input id="HomePhoneNumber" type="text" value="" name="HomePhoneNumber" data-val-required="Please enter your phone number." data-val-regex-pattern="^[0]\d{9}$" data-val-regex="Please enter your phone number, including the area code" data-val="true">
<br>
<input id="BundleWithHomePhone" type="checkbox" value="true" name="BundleWithHomePhone" data-val-required="The BundleWithHomePhone field is required." data-val="true">
<input type="hidden" value="false" name="BundleWithHomePhone">
Bundle with Home Phone?
<br>
<input id="CheckServiceAvailabilityButton" type="submit" value="Check">
</div>
<div id="Div1">
</div>
</div>

另外,如果我更改视图以使用 HTML 帮助程序,它会毫无问题地命中控制器:

<div id="Div1">
    <h1>
        Based on your location we offer different plans</h1>
    <% using (Html.BeginForm("AvailablePlans", "ServiceCheck"))
       { %>
            <%=Html.ValidationSummary(true)%>
            Your Phone Number:
            <%=Html.TextBoxFor(m => m.HomePhoneNumber)%><br />
            <%=Html.CheckBoxFor(m => m.BundleWithHomePhone)%>Bundle with Home Phone? <br/>
            <input id="Submit1" type="submit" value="Check"/>
    <% } %>
</div>

请帮忙!!!

I'm having issues posting a model to a controller action using the AJAX helper. The controller action doesn't get hit at all.

Code:

View:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ServiceNumberModel>" %>

<div id="ServiceNumberPanel">
    <h1>
        Based on your location we offer different plans</h1>
    <% using (Ajax.BeginForm("AvailablePlans", "ServiceCheck", new AjaxOptions { UpdateTargetId = "AdslPlansPanel", HttpMethod = "Post" } ))
       { %>
            <%=Html.ValidationSummary(true)%>
            Your Phone Number:
            <%=Html.TextBoxFor(m => m.HomePhoneNumber)%><br />
            <%=Html.CheckBoxFor(m => m.BundleWithHomePhone)%>Bundle with Home Phone? <br/>
            <input id="CheckServiceAvailabilityButton" type="submit" value="Check"/>
    <% } %>
</div>

Model:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace ServiceCheck
{
    public class ServiceNumberModel
    {
        [Required(ErrorMessage = "Please enter your phone number.")]
        [RegularExpression(@"^[0]\d{9}$", ErrorMessage = "Please enter your phone number, including the area code")]
        [DisplayName("Phone Number:")]
        public string HomePhoneNumber { get; set; }


        public bool BundleWithHomePhone { get; set; }
    }
}

Controller:

 [HttpPost]
        public ActionResult AvailablePlans(ServiceNumberModel serviceNumberModel)
        {
            if (serviceNumberModel.BundleWithHomePhone)
                return ReturnAvailableBundledPlans(serviceNumberModel);

            return View("Index");
        }

Generated HTML:

<div id="ServiceNumberPanel">
    <h1>
        Based on your location we offer different plans</h1>
    <form action="/ServiceCheck/AvailablePlans" data-ajax="true" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-update="#AdslPlansPanel" id="form0" method="post">
            Your Phone Number:
            <input data-val="true" data-val-regex="Please enter your phone number, including the area code" data-val-regex-pattern="^[0]\d{9}$" data-val-required="Please enter your phone number." id="HomePhoneNumber" name="HomePhoneNumber" type="text" value="" /><br />
            <input data-val="true" data-val-required="The BundleWithHomePhone field is required." id="BundleWithHomePhone" name="BundleWithHomePhone" type="checkbox" value="true" /><input name="BundleWithHomePhone" type="hidden" value="false" />Bundle with Home Phone? <br/>

            <input id="CheckServiceAvailabilityButton" type="submit" value="Check"/>
    </form>
</div>

Behaviour:
When I click on submit, I get 'Resource cannot be found error':

The resource cannot be found. Description: HTTP 404. The resource you
are looking for (or one of its dependencies) could have been removed,
had its name changed, or is temporarily unavailable. Please review
the following URL and make sure that it is spelled correctly.

Requested URL: /ServiceCheck

Interestingly when I look at the source using firebug, I cannot see the form tags:

<div class="ExistingContent">
<h1>ServiceCheck</h1>
<div id="ServiceNumberPanel">
<div id="ServiceNumberPanel">
<h1> Based on your location we offer different plans</h1>
Your Phone Number:
<input id="HomePhoneNumber" type="text" value="" name="HomePhoneNumber" data-val-required="Please enter your phone number." data-val-regex-pattern="^[0]\d{9}$" data-val-regex="Please enter your phone number, including the area code" data-val="true">
<br>
<input id="BundleWithHomePhone" type="checkbox" value="true" name="BundleWithHomePhone" data-val-required="The BundleWithHomePhone field is required." data-val="true">
<input type="hidden" value="false" name="BundleWithHomePhone">
Bundle with Home Phone?
<br>
<input id="CheckServiceAvailabilityButton" type="submit" value="Check">
</div>
<div id="Div1">
</div>
</div>

Also, if I change the view to use the HTML helper it hits the controller without issue:

<div id="Div1">
    <h1>
        Based on your location we offer different plans</h1>
    <% using (Html.BeginForm("AvailablePlans", "ServiceCheck"))
       { %>
            <%=Html.ValidationSummary(true)%>
            Your Phone Number:
            <%=Html.TextBoxFor(m => m.HomePhoneNumber)%><br />
            <%=Html.CheckBoxFor(m => m.BundleWithHomePhone)%>Bundle with Home Phone? <br/>
            <input id="Submit1" type="submit" value="Check"/>
    <% } %>
</div>

Please help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好听的两个字的网名 2024-12-16 10:03:35

我设法找到了问题。

在我使用的母版页(在多个应用程序之间共享)中,有人添加了一个胭脂表单:

<body>
    <form id="form1" runat="server">
    <uc1:header id="Header1" runat="server" />
    <div class="ExistingContent">
        <asp:contentplaceholder id="MainContent" runat="server" />
    </div>
    <uc2:footer id="Footer1" runat="server" />
    </form>
</body>

这意味着我试图从表单内提交表单,而框架默认为父级。

一旦移除,一切就开始按预期工作。

I managed to find the issue.

In the master page I was using, which is shared across a number of applications, someone had added a rouge form:

<body>
    <form id="form1" runat="server">
    <uc1:header id="Header1" runat="server" />
    <div class="ExistingContent">
        <asp:contentplaceholder id="MainContent" runat="server" />
    </div>
    <uc2:footer id="Footer1" runat="server" />
    </form>
</body>

This meant that I was trying to submit a form from within a form and the framework was defaulting to the parent.

Once removed, everything began to work as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文