2012-05-30 31 views
2

我正在使用MVC 3和Razor,嘗試從加載部分視圖的telerik窗口(telerik.window.create)將表單發回控制器。我不知道如何發佈這個病,只需按照執行順序發佈代碼,並在我去的時候解釋它。MVC 3 Razor - 表單不回發給控制器

首先錨被點擊,和的onClick調用:

function showScheduleWindow(action, configId) { 
    var onCloseAjaxWindow = function() { var grid = $("#SubscriptionGrid").data("tGrid"); if (grid) { grid.rebind(); } }; 
    CreateAjaxWindow("Create Schedule", true, false, 420, 305, "/FiscalSchedule/" + action + "/" + configId, onCloseAjaxWindow); 
} 

而且CrateAjaxWindow方法:

function CreateAjaxWindow(title, modal, rezible, width, height, url, fOnClose) { 
    var lookupWindow = $.telerik.window.create({ 
     title: title, 
     modal: modal, 
     rezible: rezible, 
     width: width, 
     height: height, 
     onClose: function (e) { 
      e.preventDefault(); 
      lookupWindow.data('tWindow').destroy(); 
      fOnClose(); 
     } 
    }); 

    lookupWindow.data('tWindow').ajaxRequest(url); 
    lookupWindow.data('tWindow').center().open(); 
} 

這裏是正在裝入的局部視圖:

@model WTC.StatementAutomation.Web.Models.FiscalScheduleViewModel 
@using WTC.StatementAutomation.Model 
@using WTC.StatementAutomation.Web.Extensions   

@using (Html.BeginForm("Create", "FiscalSchedule", FormMethod.Post, new { id = "FiscalScheduleConfigForm" })) 
{ 
    <div id="FiscalScheduleConfigForm" class="stylized"> 
     <div class="top"> 
      <div class="padding"> 
       Using fiscal year end: @Model.FiscalYearEnd.ToString("MM/dd") 
      </div> 
      <div class="padding Period"> 
       <table border="0"> 
        <tr> 
         <th style="width: 120px;"></th>     
         <th>Effective Date</th> 
         <th>Next Run</th> 
         <th>Start From Previous</th> 
        </tr> 
        <tr> 
         <td> 
          @Html.CheckBoxFor(m => m.HasMonthly) 
          <label>Monthly</label> 
         </td> 
         <td> 
          @{ var month = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.Monthly);} 
          @month.BaseSchedule.StartDate.ToString("MM/01/yyyy") 
         </td> 
         <td> 
          @month.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy") 
         </td> 
         <td class="previous"> 
          @(month.HasRun ? Html.CheckBoxFor(m => month.BaseSchedule.StartFromPreviousCycle, new { @disabled = "disabled", @readonly = "readonly" }) : Html.CheckBoxFor(m => month.BaseSchedule.StartFromPreviousCycle))  
         </td> 
        </tr> 
        <tr> 
         <td> 
          @Html.CheckBoxFor(m => m.HasQuarterly) Quarterly 
         </td> 
         <td> 
          @{ var quarter = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.Quarterly);} 
          @quarter.BaseSchedule.StartDate.ToString("MM/01/yyyy") 
         </td> 
         <td> 
          @quarter.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy") 
         </td> 
         <td class="previous"> 
          @(quarter.HasRun ? Html.CheckBoxFor(m => quarter.BaseSchedule.StartFromPreviousCycle, new { @disabled = "disabled", @readonly = "readonly" }) : Html.CheckBoxFor(m => quarter.BaseSchedule.StartFromPreviousCycle))  
         </td > 
        </tr> 
        <tr> 
         <td> 
          @Html.CheckBoxFor(m => m.HasAnnual) Annual 
         </td> 
         <td> 
          @{ var annual = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.Annual);} 
          @annual.BaseSchedule.StartDate.ToString("MM/01/yyyy") 
         </td> 
         <td> 
          @annual.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy") 
         </td> 
         <td class="previous"> 

          @(annual.HasRun ? Html.CheckBoxFor(m => annual.BaseSchedule.StartFromPreviousCycle, new { @disabled = "disabled", @readonly = "readonly" }) : Html.CheckBoxFor(m => annual.BaseSchedule.StartFromPreviousCycle))  
         </td> 
        </tr> 
        <tr> 
         <td> 
          @Html.CheckBoxFor(m => m.HasSemiAnnual) Semi-annual 
         </td> 
         <td> 
          @{ var semi = Model.GetForFiscalPeriod(FiscalPeriodStatementSchedule.FiscalPeriod.SemiAnnual);} 
          @semi.BaseSchedule.StartDate.ToString("MM/01/yyyy") 
         </td> 
         <td> 
          @semi.BaseSchedule.NextScheduleRun.ToString("MM/dd/yyyy") 
         </td> 
         <td class="previous"> 

          @(semi.HasRun ? Html.CheckBoxFor(m => semi.BaseSchedule.StartFromPreviousCycle, new { @disabled = "disabled", @readonly = "readonly" }) : Html.CheckBoxFor(m => semi.BaseSchedule.StartFromPreviousCycle))  

         </td> 
        </tr>  
       </table> 
      </div> 
      <div class="padding StartDay"> 
       <span>Run on day:</span> 
       @Html.TextBoxFor(model => model.StartDay) 
       <span>of every period.</span> 
      </div> 
     </div> 
     <div class="bottom"> 
      <div class="padding"> 
       <div style="float: left;"> 
        @if (Model.ShowSuccessSave) 
        { 
         <div id="successSave" class="label">Changes saved succesfully</div> 
        } 
        @Html.ValidationSummary(true) 
        @Html.HiddenFor(x => x.SubscriptionId) 
        @Html.HiddenFor(x => x.DeliveryConfigurationId) 
        @Html.HiddenFor(x => x.FiscalYearEnd) 
       </div> 
       <a id="saveSchedule" class="btn" href="">Save</a> 
      </div> 
     </div> 
    </div> 
} 

<script type="text/javascript"> 
    $(function() { 
     $('a#saveSchedule').click(function() { 
      $(this).closest("form").submit(); 
      return false; 
     }); 
    }); 
</script> 

最後是控制器方法:

[HttpPost] 
public ActionResult Create(FormCollection formValues, int subscriptionId, int deliveryConfigurationId, int startDay, DateTime fiscalYearEnd) 
{ 

    if (ModelState.IsValid) 
    { 

     var selectedSchedules = GetCheckedSchedulesFromForm(formValues); 
     var startFromPrevious = GetFromPreviouSelections(formValues); 
     this.AddModelErrors(_fiscalScheduleService.AddUpdateSchedules(selectedSchedules, subscriptionId, deliveryConfigurationId, startDay, startFromPrevious));    
    } 


    return new RenderJsonResult { Result = new { success = true, action = ModelState.IsValid ? "success" : "showErrors", 
            message = this.RenderPartialViewToString("_FiscalScheduleConfigForm", 
            BuildResultViewModel(deliveryConfigurationId, subscriptionId, fiscalYearEnd, ModelState.IsValid)) } };       

} 

正如你所看到的,我正在使用jQuery發回控制器,我已經在應用程序中多次完成該操作,這似乎正常工作。但是在這種形式下,出於某種原因,它根本不會回發或者進入Create方法。我猜測它與控制器方法上的參數有關。但我對MVC(來自ASP.NET世界)相當陌生,所以我不確定我在這裏做錯了什麼。任何幫助將非常感激!

+0

它看起來像你的CreateAjaxWindow

從改變(title,modal,rezible,width,height,url,fOnClose)fnuction不會調用url。在該功能內部,不使用網址。這可能是問題。讓我知道如果這是它,我會張貼作爲答案。 – MikeTWebb

+0

漂亮!大聲笑其實它只是在我複製和粘貼時被切斷。我更新了我的問題。 – Brandon

+0

LOL ...甜美。那麼對你的問題有什麼想法? – MikeTWebb

回答

5

我能得到通過修改朝九特派的textboxfor它張貼到控制器:

@Html.TextBoxFor(model => model.StartDay) 

要:

@Html.TextBoxFor(model => model.StartDay, new { id = "startDay" }) 
1

我的猜測是你在IIS中的虛擬目錄上運行?你產生的這個網址可能是罪魁禍首。

點擊F12,查看網絡選項卡(並啓用跟蹤),看看它試圖請求。

不是通過文字建立鏈接,爲什麼不使用@Url.Action()?您可以將其存儲在a標記的屬性中(例如,在名爲data-url的屬性中),然後使用該信息撥打電話。用jQuery取出屬性非常簡單,如下所示:

$('.your-link-class').click(function(){ 
    var url = $(this).attr('data-url'); 
    // proceed with awesomesauce 
}); 

會是這樣的嗎?

[無恥自我插入]就控制器動作簽名而言,如果可以的話,您可能需要查看模型綁定。一個簡單的課程和許多你的頭痛將消失。你可以閱讀更多here,閱讀模型綁定的部分。有不同方法的可下載樣本。

乾杯。

+0

感謝您的回覆。現在我只是在Cassini中運行它,它是VS 2010啓動的內置IIS實例。它將其作爲獨立網站啓動(即http:// localhost:58830)。我喜歡你使用@ Url.Action()屬性的建議,絕對清晰!感謝那!我正在閱讀該網站,謝謝你的。實際上我終於能夠把它發佈給控制器,但我有點困惑,爲什麼這會產生變化,並會提供你所有可能對此產生的任何評論(見下文) – Brandon

+0

我能夠得到它通過修改startDay的textboxfor發佈到控制器:更改自:@ Html.TextBoxFor(model => model.StartDay)To:@ Html.TextBoxFor(model => model.StartDay,new {id =「startDay」})但我不知道爲什麼修復它。有任何想法嗎?似乎它可能區分大小寫,但其他表單值與不同的情況一起工作。 – Brandon

+0

是的,這個作品的原因是因爲模型綁定是大小寫敏感的。你的動作的int startDay與StartDay不一樣,它是@Model.StartDay爲你生成的MVC。因爲您使用裸體參數而不是模型綁定到類,方法簽名實際上是不同的,並且綁定器無法整理您的參數。這可能是爲什麼該行動沒有被引用。希望有助於一些! – MisterJames

相關問題