2012-07-13 33 views
0

我創建了一個工作的MVC頁面,它帶回了一組數據,顯示一個特定的報告(頂級客戶的花費量)。我試圖添加兩個jQuery datepicker文本框,這將允許用戶通過開始和結束日期綁定該報告。它目前正在工作,因爲我有一個綁定了jQuery click事件的HTML錨標記,它啓動了一個處理程序。該處理程序將開始/結束日期扔給我的控制器中的HttpPost ActionResult方法,並獲取更新後的數據集。MVC 3剃刀 - 創建報告使用2 datepickers綁定數據從模型

問題是當我嘗試返回數據到視圖(而不是返回內容(「真」)),沒有數據返回,並且處理程序給我我的OnFailure消息。是否有可能以這種方式將數據傳遞給視圖,還是需要使用Html.BeginForm類型的方法重新編寫它?

非常感謝!

下面的代碼: (在我看來)

<script type="text/javascript"> 
$(document).ready(function() { 
    $(".datepicker").datepicker(); 

    $('#submit').click(function() { 
     SetDateHandler($('#startdate').val(), $('#enddate').val(), "company"); 
    }); 

    function SetDateHandler(bounddate1, bounddate2, datetype) { 
     $.ajax({ 
      //url: '/Projects/TestArray', 
      url: '@Url.RouteUrl("SetBoundingDate")', 
      contentType: "application/json; charset=utf-8", 
      dataType: 'json', 
      success: OnComplete, 
      data: JSON.stringify({ startdate: bounddate1, enddate:bounddate2, type: datetype }), 
      traditional: true, 
      type: "POST", 
      error: OnFail 
     }); 

     return false; 
    } 
    function OnComplete(result) { 
     //indow.location.href = window.location.href; 
     alert("yay!"); 
     return true; 
    } 
    function OnFail(result) { 
     alert('Request Failed'); 
     return false; 
    } 

}); 
</script> 

<h2>Top Companies by Total Price for Won Quotes</h2> 

<div style="float:right; width: 350px; margin: 15px;"> 
    <span style="float:right; margin-right: 50px;">Start Date: <input class="datepicker" id="startdate" /></span> 
    <br /> 
    <span style="float:right; margin-right: 50px;">End Date: <input class="datepicker" id="enddate" /></span> 
    <br /> 
    <a href="#" id="submit">Submit</a> 
</div> 

定製路線: routes.MapRoute( 名稱: 「SetBoundingDate」, 網址: 「AjaxSetBoundingDate」, 默認:新{控制器= 「Reports」,action =「SetBoundingDates」} );

在我的報告控制器:

[HttpPost] 
public ActionResult SetBoundingDates(String startdate, String enddate, String type) 
{ 
    Schedule.Models.Reports.TopCompaniesModel Companies = new Models.Reports.TopCompaniesModel(); 

    ActionResult view; 

    if (type == "company") 
    { 
     view = TopCompaniesReport(startdate, enddate); 
    } 
    else 
    { 
     view = TopContactsReport(startdate, enddate); 
    } 
    //return View(view); 
    return Content("true"); 
} 

同樣,如果你已經讀到這裏,我讓我的控制器方法與觀點正確填寫,但底部,當我嘗試返回查看,它的錯誤,如果我返回內容(「真」),它的工作原理,但沒有任何更改頁面上。

謝謝!

回答

1

再次感謝弗洛姆試圖幫助。我的方法是錯誤的,我最終發現我需要切換到剃刀回發方法。這種方式實際上也很容易。

這是我所採取的方式:

<div style="float:right; width: 550px; margin: 15px;"> 
     @using (Html.BeginForm(ViewContext.RouteData.GetRequiredString("action"), "Reports", FormMethod.Post, new { id = "TheForm" })) 
     { 
      <span style="float:right;"> 

        <div style="margin-right: 50px;float:right;">Start Date: <input class="datepicker" name="startdate" id="startdate" /></div> 
        <div style="margin-right: 50px;float:right; margin-top: 5px; clear: both;">End Date: <input class="datepicker" name="enddate" id="enddate" /></div> 

        <input id="submit" name="submit" type="submit" value="Update" style="margin-top: 10px; clear: both;float:right;" /> 

      </span> 
      <span style="float: left;"> 
       @Html.DropDownList("DateDdl", (SelectList)ViewBag.DateDropDown) 
      </span>   

     } 
    </div> 

其實我喜歡這種方式要好得多,因爲你可以看到,我通過它的電流控制器名字調回。通過這種方式,我將此功能放入共享的局部視圖中,我可以在報告區域內的任何視圖上重複使用該功能。

希望這可以幫助別人!