【问题标题】:Asp.Net MVC Ajax Form Not Posting to ActionAsp.Net MVC Ajax 表单未发布到操作
【发布时间】:2012-06-03 04:32:32
【问题描述】:

我在使用 Ajax 表单时遇到了一点问题。毫无疑问,我可能做得有点倒退。基本上,当我点击表单中的提交按钮时,什么都没有发生。我已经调试过,但它似乎没有发布到 action 方法。它实际上什么也没做。

到目前为止,这是我的代码:

基本上,我使用DetailedBreakdownReportRequest 模型从表单发布信息。由于我的页面显示了初始未过滤结果的列表,因此该模型可以很好地通过。

DetailedBreakdownReport 操作方法:

[HttpPost]
public ActionResult DetailedBreakdownReport(DetailedBreakdownReportRequest reportRequest, FormCollection formVariables)
{
    return View(reportRequest);
}

详细分解报告视图

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage<MyApp.Data.AdvancedReports.AdvancedReports.DetailedBreakdownReports.DetailedBreakdownReportRequest>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Detailed Breakdown Report
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script language="javascript" type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.4.1.min.js")%>"></script>
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

 <% using (Ajax.BeginForm("DetailedList", "Reports",
        new AjaxOptions
        {
          UpdateTargetId = "panelBreakdownList",
          InsertionMode = InsertionMode.Replace
        },
        new { id = "SearchForm" })) %>
<% { %>

                <div style="position: absolute; top: 0px; height: 30px; right: -12px; margin-top: 8px;
    width: 250px; padding-left: 00px; vertical-align: middle; display: inline-block;">
        <input id="searchField" name="searchField" style="padding-left: 5px; position: relative;
        float: left; top: 3px; margin: 0px; border: 1px solid #DDDDDD; height: 19px;
        width: 200px;" />
        <%: Html.HiddenFor(m => m.ToDate) %>
        <%: Html.HiddenFor(m => m.FromDate) %>
        <%: Html.HiddenFor(m => m.Currency) %>
        <%: Html.HiddenFor(m => m.ReportType) %>
        <!--<input type="image" src="/Content/Images/search-form-submit.png" style="border: 0px;
        height: 27px; position: relative; left: -8px; margin: 0x; float: left;" />-->
        <input type="submit" value="Search" style="border: 0px;
        height: 27px; position: relative; left: -8px; margin: 0x; float: left;" />
    </div>
<% } %>

 <div id="panelBreakdownList" style="position: relative; z-index: 0;">
        <% Html.RenderAction("DetailedList", new { ToDate = Model.ToDate, FromDate = Model.FromDate, Currency = Model.Currency, ReportType = Model.ReportType }); %>
</div>


</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="Header" runat="server">
<h1>Detailed Breakdown Report</h1>
<h2><%: Model.ToDate.ToString("dd-MMM-yyyy") %> to <%: Model.FromDate.ToString("dd-MMM-yyyy")%></h2>
</asp:Content>

这是从上述页面调用的DetailedList操作,它是视图:

[HttpPost]
public ActionResult DetailedList(DateTime ToDate, DateTime FromDate, string Currency, string ReportType, FormCollection formVariables)
{
    DetailedBreakdownReportRequest reportRequest = new DetailedBreakdownReportRequest()
    {
        ToDate = ToDate,
        FromDate = FromDate,
        Currency = Currency,
        ReportType = ReportType,
        UserId = UserServices.CurrentUserId
    };

    DrilldownReportEngine re = new DrilldownReportEngine();
    DetailedBreakdownReport report = null;

    if (formVariables.HasKeys())
    {
        reportRequest.searchTerm = formVariables["searchField"];
        report = re.GetDetailedBreakdown(reportRequest);
    }
    else
    {
        report = re.GetDetailedBreakdown(reportRequest);
    }

    return PartialView(report);
}

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Data.AdvancedReports.AdvancedReports.DetailedBreakdownReports.DetailedBreakdownReport>" %>

    <% if (Model.HasData)
       { %>

       <% if (Model.ShopBreakdown != null)
          { %>

          <h2>Shop Breakdown Report</h2>  
          <p>Click on a shop's name to see additional information.</p>
          <div id="shopGrid" style="float:left; width: 400px;">
              <table width="400px">
              <% foreach (var shop in Model.ShopBreakdown)
                 { %>
                <tr>
                    <td colspan="3"><h2><%: Html.ActionLink(shop.Shop.Name + " >>", "ShopDashboard", new { ShopId = shop.Shop.ShopID, Currency = Model.Currency, toDate = Model.ToDate.ToString(), fromDate = Model.FromDate.ToString(), percentageOfSales = shop.RevenuePercentageOfSales })%></h2></td>
                </tr>
                <tr>
                    <td><p class="labels">Units Sold:</p></td>
                    <td><b style="font-size:larger;"><%: shop.UnitsSoldPerShop%></b></td>
                    <td rowspan="2" align="center"><h2><%: String.Format("{0:0.0%}", shop.RevenuePercentageOfSales)%></h2> of Total Revenue</td>
                </tr>
                <tr>
                    <td><p class="labels">Revenue Earned:</p></td>
                    <td><b style="font-size:larger;"><%: String.Format("{0:0.00}", shop.TotalRevenuePerShop)%></b></td>
                </tr>
                <tr>
                    <td colspan="3">&nbsp;</td>
                </tr>
                <% } %>
              </table>
          </div>
       <% } %>



       <% if (Model.ProductBreakdown != null)
          { %>
              <% foreach (var product in Model.ProductBreakdown)
                 { %>
                       <div id="ProductGrid" style="float:left; width: 500px;">
                     <div>
                     <h3><%: Html.ActionLink(product.Product.Name + " >>", "ProductDashboard", new { ProductId = product.Product.ProductID, Currency = Model.Currency, toDate = Model.ToDate.ToString(), fromDate = Model.FromDate.ToString(), percentageOfSales = product.RevenuePercentageOfSales })%></h3>
                     </div>
                     <div style="float:left; width: 200px;">
                            <p class="labels">Units Sold: </p><b style="font-size:larger;"><%: product.TotalUnitsSoldPerProduct %></b>
                            <br />
                            <p class="labels">Revenue Earned: </p><b style="font-size:larger;"><%: String.Format("{0:0.00}", product.OverallTotalRevenuePerProduct)%></b>
                     </div>
                     <div style="float: left; text-align: center;">
                            <h2><%: String.Format("{0:0.0%}", product.RevenuePercentageOfSales)%></h2> of Total Revenue
                     </div>

                </div>

                <% } %>

                <div style="clear:both;" />
                <br />


     <% } %>
    <% } %>

正如我上面所说,在第一次加载时,页面显示正常,它显示了未经过滤的结果视图。当我在文本框中输入一个值并单击提交时,什么都没有发生,我发现这很难调试。至少如果它做了什么我会有一些工作。谁能看看我在这里做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-ajax ajaxform


    【解决方案1】:

    如我所见,您希望在操作 detailList 中有一些不可为空的变量,这些变量是表单所期望的。

    [HttpPost]
    public ActionResult DetailedList(DateTime ToDate, DateTime FromDate, string Currency, string ReportType, FormCollection formVariables)
    {
        ......
    }
    

    但是您只是在表单集合中发送搜索字段。表单中的 ToDate、FromDate、Currency 变量在哪里?

    我认为你应该例如创建一个表单模型

    DetailedListSearchModel
        DateTime ToDate
        DateTime FromDate
        string Currency
        string ReportType
        string Searchfield
    

    并使搜索成为局部视图。当部分视图使用默认值渲染时,只需传递默认值,然后在表单中执行。

    然后你将把这些价值观付诸实践,比如 [HttpPost]

    public ActionResult DetailedList(DetailedListSearchModel model)
    {
        ......
    }
    

    你可以使用这个模型像

    <%= Html.LabelFor(m => m.Searchfield) %>
    <%= Html.TextBoxFor(m => m.Searchfield, new { @class = "css classes", maxlength = "1000" })%>
    <%= Html.ValidationMessageFor(m => m.Searchfield) %>
    
    
    
    
    $(function() {
          $('form#SearchForm').find('a.submit-link').click( function() {
               $('form#SearchForm').trigger('submit');
          }).show();
      }
    

    并将搜索按钮更改为 a。

    <a href="#" class="submit-link">Search</a>
    

    【讨论】:

    • 感谢您的快速回复。不通过任何东西是完全合理的。我已经更新了我的问题以包含隐藏字段,只是想看看这在更新我的代码之前是否会产生任何影响,就像你上面所说的那样。单击时,提交按钮仍然没有任何作用。还有其他想法吗?
    • 你应该提交表单,因为它是 ajax 表单。我包括了一个样本
    猜你喜欢
    • 2012-05-10
    • 2014-04-04
    • 2019-03-30
    • 2013-07-25
    • 2015-12-14
    • 2018-09-19
    • 2019-01-26
    • 1970-01-01
    • 2022-07-15
    相关资源
    最近更新 更多