2012-03-06 73 views
1

作爲我當前項目的一部分,我已經陷入了一個巨大的MVC Web表單中,我對此知之甚少,並且不得不對其進行相當嚴格的調試。我有很多WebForms的經驗,但很少與MVC,所以請原諒我,如果以下不明確,或者是提出愚蠢的問題。ASP.net 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代碼

如果(Model.Visible) {%>

<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)%>

乾杯, 馬特

+0

您能詳細說明一下表單元素的開啓/關閉位置嗎? – Henry 2012-03-06 10:41:17

+0

這是基於複選框。默認情況下,表單元素是type =「hidden」,當選中該框時,通過javascript刪除它。很明顯,在這種情況下,該元素處於未隱藏狀態,並且保持未隱藏狀態,但其中的任何文本框都不會保留其值。 – 2012-03-06 10:46:07

+0

好吧,這聽起來像POST值可能沒有正確綁定。你可以提供更多的信息:(1)視圖代碼生成特定的表單元素(2)它綁定的屬性 – Henry 2012-03-06 11:00:47

回答

0

嘗試更換:

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

有:

<%= Html.EditorForModel() %> 

,或者如果你的模式類型爲而不是BlackWhiteListConfigViewData你可以指定它:

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

而且你的編輯模板中,你似乎可以用一些自定義的助手如Html.EditorForNested。確保您的輸入字段名稱尊重conventions for binding to a list

+0

EditorForModel仍會導致相同的錯誤。和EditorFor一樣。我還沒有檢查綁定約定 - 現在就這樣做。 – 2012-03-06 12:14:25