2

Ajax フォームに少し問題があります。私はおそらくそれを少し後ろ向きにやったに違いありません。基本的に、フォームの送信ボタンを押しても何も起こりません。デバッグしましたが、アクション メソッドに投稿されていないようです。文字通り何もしません。

これまでの私のコードは次のとおりです。

基本的には、DetailedBreakdownReportRequest のモデルを持つフォームから情報を投稿します。私のページには最初のフィルタリングされていない結果のリストが表示されるため、このモデルは正常に通過しています。

DetailedBreakdownReport アクション メソッド:

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

DetailedBreakdownReport ビュー

<%@ 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 />


     <% } %>
    <% } %>

上で述べたように、最初の読み込みでは、ページは正常に表示され、フィルタリングされていない結果のビューが表示されます。テキスト ボックスに値を入力して [送信] をクリックしても何も起こらず、デバッグが困難です。少なくともそれが何かをしたのなら、私は何か仕事をするでしょう。私がここで何か間違っているかどうか誰にもわかりますか?

4

1 に答える 1

1

私が見るように、あなたはフォームから期待されるアクションdetailedListのいくつかのnull許容でない変数を期待します。

[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 href="#" class="submit-link">Search</a>
于 2012-05-28T22:25:31.120 に答える