【问题标题】:All model and Formcollection values are null, blank or don't exist in Firefox or Chrome所有模型和 Formcollection 值为 null、空白或在 Firefox 或 Chrome 中不存在
【发布时间】:2012-10-09 21:26:16
【问题描述】:

在调试期间,我的 MVC 模型和 Formcollection 为空白,在 FireFox (15) 或 Chrome(最新版本)中没有值。

在使用 IE (9) 进行调试期间,我可以看到这些值。

您知道解决方案是什么吗?这对于无法针对这些浏览器进行任何编程的面向公众的网站来说是非常严重的。

这是我的观点...

@model PDFConverterModel.ViewModels.ViewModelTemplate_Guarantors

@{
    ViewBag.Title = "BHG :: PDF Generator";
}
<h2>@ViewBag.Message</h2>

<div>

    <table style="width: 1000px">
        <tr>
            <td colspan="5">
                <img alt="BHG Logo" src="~/Images/logo.gif" />
            </td>
        </tr>

        @using (Html.BeginForm("ProcessForm", "Home", FormMethod.Post))
        {  
            <tr>
                <td>
                @(Html.Kendo().IntegerTextBox()
                        .Name("LoanID")
                        .Placeholder("Enter Loan ID")
                )
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.LoanType)
                    @Html.DisplayFor(model => model.LoanType)
                </td>
                <td>
                    <label for="ddlDept">Department:</label>
                    @(Html.Kendo().DropDownList()
                            .Name("ddlDept")
                            .DataTextField("DepartmentName")
                            .DataValueField("DepartmentID")
                            .Events(e => e.Change("Refresh"))
                            .DataSource(source =>
                            {
                                source.Read(read =>
                                {
                                    read.Action("GetDepartments", "Home");
                                });
                            })
                    )
                </td>
            </tr>

            if (Model.ShowGeneratePDFBtn == true)
            {
                if (Model.ErrorT == string.Empty)
                {
            <tr>
                <td colspan="5">
                    <u><b>@Html.Label("Templates:")</b></u>
                </td>
            </tr>
            <tr>
                @for (int i = 0; i < Model.Templates.Count; i++)
                {  
                    <td>
                        @Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
                        @Html.DisplayFor(model => Model.Templates[i].TemplateId)
                    </td> 
                }

            </tr>
                }
                else
                {
            <tr>
                <td>
                    <b>@Html.DisplayFor(model => Model.ErrorT)</b>
                </td>
            </tr>
                }

                if (Model.ErrorG == string.Empty)
                {
            <tr>
                <td colspan="5">
                    <u><b>@Html.Label("Guarantors:")</b></u>
                </td>
            </tr>
            <tr>
                @for (int i = 0; i < Model.Guarantors.Count; i++)
                { 
                    <td>
                        @Html.CheckBoxFor(model => Model.Guarantors[i].isChecked)
                        @Html.DisplayFor(model => Model.Guarantors[i].GuarantorFirstName)&nbsp;@Html.DisplayFor(model => Model.Guarantors[i].GuarantorLastName)
                    </td> 
                }

            </tr>
                }
                else
                {
            <tr>
                <td>
                    <b>@Html.DisplayFor(model => Model.ErrorG)</b>
                </td>
            </tr>
                }
            }   
            <tr>
                <td colspan="3">
                    <input type="submit" name="submitbutton" id="btnRefresh" value='Refresh' />
                </td>
                @if (Model.ShowGeneratePDFBtn == true)
                {
                    <td>
                        <input type="submit" name="submitbutton" id="btnGeneratePDF" value='Generate PDF' />
                    </td>
                }
            </tr>
            <tr>
                <td colspan="5">
                    @Model.Error
                </td>
            </tr>
        }
    </table>

</div>

<script type="text/javascript">

    $('btnRefresh').on('click', '#btnRefresh', function () {
        Refresh();
    });

    function Refresh() {

        var LoanID = $("#LoanID").val();

        if (LoanID != "") {
            document.forms[0].submit();
        }
        else {
            alert("Please enter a LoanId");
        }
    }
</script>

【问题讨论】:

    标签: asp.net-mvc firefox google-chrome model formcollection


    【解决方案1】:

    我知道这是一个非常古老的问题,但回答这个问题可能会帮助像在这个问题上苦苦挣扎的人。

    我遇到了类似的问题。问题出在这里:

    <table style="width: 1000px">
            <tr>
                <td colspan="5">
                    <img alt="BHG Logo" src="~/Images/logo.gif" />
                </td>
            </tr>
    
            @using (Html.BeginForm("ProcessForm", "Home", FormMethod.Post))
            {  
                <tr>
                 <td>
                    @(Html.Kendo().IntegerTextBox()
                            .Name("LoanID")
                            .Placeholder("Enter Loan ID")
                    )
                 </td>
                </tr>
            }
    </table>
    

    begin 表单后直接有&lt;tr&gt; 标签!在这种情况下,像 chrome 和 Mozilla 这样的浏览器会感到困惑。 &lt;table&gt; 标签应该在表单内。如果我们查看您的代码,这正是我所做的,&lt;table&gt; 标签在@using Html.BeginForm 之前。 Internet Explorer 以某种方式理解这一点,但其他浏览器不理解。

    当我执行检查元素时,我发现每个 &lt;tr&gt; 标记中都有一个表单标记,并且它总是将 FormCollection 返回为 null。只需在表单中定义 &lt;table&gt; 即可解决我的问题。

    所以它应该是这样的:

    <table style="width: 1000px">
                <tr>
                    <td colspan="5">
                        <img alt="BHG Logo" src="~/Images/logo.gif" />
                    </td>
                </tr>
                <tr><td>
                @using (Html.BeginForm("ProcessForm", "Home", FormMethod.Post))
                {
                 <table>  
                    <tr>
                     <td>
                        @(Html.Kendo().IntegerTextBox()
                                .Name("LoanID")
                                .Placeholder("Enter Loan ID")
                        )
                     </td>
                    </tr>
                 </table>
                }
                </td></tr> 
    </table>
    

    【讨论】:

      【解决方案2】:

      我刚刚通过实验发现了问题所在。

      Telerik MVC 小部件不发出任何 FormCollection 数据!!!!

      只有 EditorFor 和 TextBoxFor 发出这些值,以及输入按钮。

      如果我不能使用它们中的 FormCollection 值,这些小部件有什么好处????尤其是 DropDownList,我可以在其中检索数据并需要将所选值传递给其他方法。

      【讨论】:

        【解决方案3】:

        (这更适合作为评论,但我还不能评论)

        为了将来参考,这里有一个规范(W3C 可能有不同的东西)关于提交表单时提交的内容:

        http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#category-submit

        您可以查看生成的任何 HTML 以确保它被提交。您还可以使用 Fiddler 之类的东西来查看 Http 请求

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-06
          • 2013-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多