ASP.net MVC模型无法识别值

时间:2012-03-06 10:35:50

标签: asp.net-mvc

作为我当前项目的一部分,我犯了一个巨大的MVC网页形式,我对此知之甚少,并且不得不对它进行一些相当严肃的调试。我有很多WebForms经验,但很少有MVC所以请原谅我,如果以下不清楚,或者是在问愚蠢的问题。

表单的一部分是自定义.ascx控件,它从另一个URL获取信息,并根据该信息限制用户可以放入表单的该部分。可以打开和关闭所有表单元素。如果你打开这个特定的表单元素,填写并提交然后ModelState未通过其验证检查,抱怨它缺少必需的值。返回表单时打开有问题的元素,但框中没有值。如果不打开此特定元素,ModelState将正确验证。

我对MVC了解得足以理解表单元素和模型之间的映射是在幕后进行的。然而,这使得调试非常困难。我真的不知道问题是什么,或者更糟糕的是,如何调查原因是什么。任何一方的任何建议都将不胜感激。

编辑:一些代码

//start to deal with specialist form user controls        
case    "ViewData.Customer.CustomFieldTypes.BlackWhiteListConfigViewData ":        
 if (Model.Visible)
   { %>
 <div class="formItem">
   <label>
    <%      Html.RenderPartial("~/Views/Shared/EditorTemplates/BlackWhiteListConfigViewData.ascx"); %>
</div>
<% }
   else
   { %>
 <%= Html.HiddenFor(m => Model.Value) %>
<% }
 b break;     

绑定代码:

  switch (fieldType)
            {
                case FeatureFieldFactory.BlackWhiteListConfigElTypeName:
                    {
                        // create a new default model binder, and tell it which type we actually want it to bind.
                        BlackWhiteListConfigViewData model = new BlackWhiteListConfigViewData();
                        bindingContext.ModelMetadata =
                            ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(BlackWhiteListConfigViewData));

                        return base.BindModel(controllerContext, bindingContext);
                    }

编辑:BlackWhiteListConfigViewData.ascx代码

if(Model.Visible)         {%&gt;

    <script type="text/javascript">
        $(cpContext.CurrentService()).bind('onServiceAttributesReady', function (context) {
            $(document).ready(function () {
                $("#js-hook-BlackWhiteListLoading").hide();
                if (context.target.AttributeNames().length === 0) {
                    $("#js-hook-AddBlackWhiteListEntry").hide();
                    $("#js-hook-BlackWhiteListConfigTable").hide();
                    $("#js-hook-BlackWhiteListMessage").html('There are no attribute types present in the catalogue.  The Black and White lists can be configured once the catalogue is loaded.');
                    $("#js-hook-BlackWhiteListMessage").show();
                }
                else {
                    $("#js-hook-BlackWhiteListMessage").hide();
                    $("#js-hook-AddBlackWhiteListEntry").show();
                    $("#js-hook-BlackWhiteListConfigTable").show();
                }
            });
        });

        $(cpContext.CurrentService()).bind('onServiceAttributesError', function (context) {
            $(document).ready(function () {
                $("#js-hook-BlackWhiteListLoading").hide();
                $("#js-hook-AddBlackWhiteListEntry").hide();
                $("#js-hook-BlackWhiteListConfigTable").hide();
                $("#js-hook-BlackWhiteListMessage").html('No attribute types retrieved from service API.  The Black and White lists can be configured when a Build has been completed and service API is accessible.');
                $("#js-hook-BlackWhiteListMessage").show();
            });
        });

        cpContext.CurrentService().getAttributes();
    </script> 

    <div class="formItem narrow blackWhiteListConfig">
        <% using (Html.BeginCollectionItem("fields"))
           {
               Model.Value = "n/a";
        %>

            <%= Html.HiddenFor(m => Model.Id) %>
            <%= Html.HiddenFor(m => Model.Type) %>
            <%= Html.HiddenFor(m => Model.Name) %>
            <%= Html.HiddenFor(m => Model.Visible) %>
            <%= Html.HiddenFor(m => Model.DisplayName) %>
            <%= Html.HiddenFor(m => Model.Value) %>

            <table id="js-hook-BlackWhiteListConfigTable" class="configTable" style="display:none">
                <thead>
                    <tr><th class="configTableColumn">Key Attribute Type</th><th class="configTableColumn">Key Value</th><th class="transparent"></th><th class="configTableColumn">Related Attribute Type</th><th class="configTableColumn">Related Attribute Value</th><th class="configTableColumn narrow">Black</th><th class="configTableColumn narrow">White</th><th class="transparent" style="width:20px"></th></tr>
                </thead>
                <tbody class="blackWhiteListRows">
                <% foreach (BlackWhiteListEntryViewData entry in Model.Entries)
                   { %>
                        <%= Html.EditorForNested(e => entry) %>
                <% } %>
                </tbody>
            </table>

               <hr />
                <p id="js-hook-AddBlackWhiteListEntry" class="clear" style="display:none"><a  href="#">Add another entry</a></p>
                <p id="js-hook-BlackWhiteListMessage" class="clear" style="display:none">There are no attribute types present in the catalogue.  The Black and White lists can be configured once the catalogue is loaded.</p>
                <img id="js-hook-BlackWhiteListLoading" src="<%=Links.Content.images.content_loading_gif %>" alt="Loading Black and White list configuration..."
                     style="" class="clear" />
            <%= Html.ValidationMessageFor(m => Model) %>
    <% } %>

         Model.Value)%&gt;    

干杯, 马特

1 个答案:

答案 0 :(得分:0)

尝试更换:

<% Html.RenderPartial("~/Views/Shared/EditorTemplates/BlackWhiteListConfigViewData.ascx"); %>

使用:

<%= Html.EditorForModel() %>

或者如果您的模型类型不是BlackWhiteListConfigViewData,您可以指定它:

<%= Html.EditorForModel("BlackWhiteListConfigViewData") %>

同样在您的编辑器模板中,您似乎正在使用一些自定义帮助程序,例如Html.EditorForNested。确保您的输入字段名称尊重conventions for binding to a list