MVC 3 Razor - 表格不回发给控制器

时间:2012-05-30 15:25:14

标签: asp.net-mvc-3 jquery razor

我正在使用MVC 3和Razor,尝试将表单从telerik窗口(telerik.window.create)发送回控制器,该窗口加载局部视图。我不知道如何发布这样的病,只是按照执行的顺序发布代码并在我去的时候解释它。

首先单击一个锚点,然后点击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世界)相当新,所以我不确定我在这里做错了什么。任何帮助都会非常感激!

2 个答案:

答案 0 :(得分:5)

我能够通过修改startDay的textboxfor来将其发布到控制器:

更改自:

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

致:

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

答案 1 :(得分:1)

我的猜测是你在IIS中的虚拟目录上运行了吗?你生成的那个网址很可能是罪魁祸首。

点击F12,查看网络标签(并启用跟踪)并查看它尝试请求的内容。

为什么不使用@Url.Action()而不是通过文字构建链接?您可以将其存储在a标记的属性中(例如,在名为data-url的属性中),然后使用该信息进行调用。使用jQuery提取属性非常容易,如下所示:

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

这样的事情对你有用吗?

[无耻的自我插件]就控制器动作签名而言,如果可以,您可能希望查看模型绑定。一个简单的课程和许多头痛都会消失。您可以阅读更多here,阅读有关模型绑定的部分。有可下载的样本用于不同的方法。

干杯。

相关问题