2012-05-28 29 views
2

我有一個與Ajax窗體的問題。毫無疑問,我可能已經做了一些倒退。基本上,當我點擊表單中的提交按鈕時,什麼都不會發生。我已經調試過,但它似乎沒有發佈到操作方法。它從字面上什麼都不做。Asp.Net MVC阿賈克斯窗體不張貼到行動

這是到目前爲止我的代碼:

基本上我從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 /> 


    <% } %> 
    <% } %> 

正如我如上所述,在第一次加載時,頁面顯示正常,它顯示未過濾結果的看法。當我在文本框中輸入一個值並單擊提交時,什麼都不會發生,我發現很難調試。至少如果它做了什麼我會有一些工作。任何人都可以看到我在這裏做錯了什麼嗎?

回答

1

正如我所看到的,您期望在操作detailedList中有一些不可爲空的變量,這些變量是從窗體中預期的。

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

但是你只是發送表單集合中的searchfield。 ToDate,FromDate,貨幣變量在窗體中的位置在哪裏?

我想你應該forexample

DetailedListSearchModel 
    DateTime ToDate 
    DateTime FromDate 
    string Currency 
    string ReportType 
    string Searchfield 

創建formmodel,使搜索partialview。當部分視圖以默認值呈現時,只需傳遞默認值,然後在窗體中執行。

然後你會採取這種價值觀在行動像 [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> 
+0

感謝您的快速回復。對於不傳遞任何東西非常有意義。我已經更新了我的問題以包含隱藏字段,只是爲了查看這是否會在更新我的代碼之前有所作爲,如上所述。點擊時,提交按鈕仍然不起作用。任何其他想法? – 109221793

+0

您應該提交表單,因爲它是ajax表單。我包括一個樣本 – gandil