asp.net-mvc-3 - MVC 3 Razor - 表单未发回 Controller

标签 asp.net-mvc-3 jquery razor

我正在使用 MVC 3 和 Razor,尝试从加载部分 View 的 Telerik 窗口 (telerik.window.create) 将表单发送回 Controller 。我不确定如何发布此内容,因此只需按执行顺序发布代码并边解释边进行即可。

首先单击 anchor ,然后 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();
}

这是正在加载的部分 View :

@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>

最后是 Controller 方法:

[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 回发到 Controller ,我已经在应用程序中多次这样做了,这似乎正常工作。但对于这种形式,由于某种原因,它根本不会回发或单步进入 Create 方法。我推测这与 Controller 方法上的参数有关。但我对 MVC 相当陌生(来自 ASP.NET 世界),所以我不太确定我在这里做错了什么。任何帮助将不胜感激!

最佳答案

我能够通过修改 startDay 的文本框将其发布到 Controller :

更改自:

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

致:

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

关于asp.net-mvc-3 - MVC 3 Razor - 表单未发回 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10819370/

相关文章:

asp.net-mvc - 如何在 ASP.NET MVC 3 WebGrid 列名(标题)中添加 Html

asp.net - 如何在 Asp.Net MVC3 中单击注销时删除 cookie

javascript - 从函数触发日期选择器不起作用

c# - 如何在 Blazor 中将带有参数的 onclick 添加到按钮?

asp.net-mvc - ASP.NET MVC - 什么是 UrlRoutingModule?

c# - 表单验证只允许英文字母字符

javascript - 如何使用 jQuery 关闭窗口

php - 根据ID值选择数据

jquery - MVC4 表单序列化到 Controller 不起作用

c# - 我将如何在 mvc 4 中将以下语句的值设置为零