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

时间:2011-10-11 00:32:40

标签: asp.net-mvc asp.net-ajax http-post

我在使用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:

<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.资源你   正在寻找(或其中一个依赖)可能已被删除,   更改名称,或暂时不可用。请查阅   以下网址,并确保拼写正确。

     

请求的网址:/ 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>

请帮助!!!

1 个答案:

答案 0 :(得分:0)

我设法找到了问题。

在我使用的母版页中,在多个应用程序中共享,有人添加了一个胭脂形式:

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

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

一旦删除,一切都开始按预期工作。