我正在使用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世界)相當陌生,所以我不確定我在這裏做錯了什麼。任何幫助將非常感激!
它看起來像你的CreateAjaxWindow
從改變(title,modal,rezible,width,height,url,fOnClose)fnuction不會調用url。在該功能內部,不使用網址。這可能是問題。讓我知道如果這是它,我會張貼作爲答案。 – MikeTWebb
漂亮!大聲笑其實它只是在我複製和粘貼時被切斷。我更新了我的問題。 – Brandon
LOL ...甜美。那麼對你的問題有什麼想法? – MikeTWebb