的我有一個簡單強類型的視圖,並且當我提交表單,視圖模型的特性之一是有時NULL對於某些數據綁定;大多數數據工作正常。我已經削減了我的看法爲仍然重現錯誤的最簡單的形式:模型在MVC不工作取決於GUID項
@model eNPSWeb.Models.Survey_c.StartModel
@{
Layout = null;
}
<html>
<head>
@Html.DevExpress().GetScripts(new Script { ExtensionSuite = ExtensionSuite.Editors })
</head>
<body>
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.EmployeeSurveyName)
<input type="submit" />
for (int x = 0; x < Model.Employees.Count(); ++x)
{
@Html.HiddenFor(model => model.Employees[x].Name)
@Html.HiddenFor(model => model.Employees[x].ExternalId)
@Html.HiddenFor(model => model.Employees[x].Included)
}
for (int x = 0; x < Model.Questions.Count(); ++x)
{
<div>
<input type="hidden" name="@string.Format("{0}a", Model.Questions[x].ExternalId)" />
<input type="hidden" name="@string.Format("{0}b", Model.Questions[x].ExternalId)" />
@Html.HiddenFor(m => m.Questions[x].Id)
@Html.HiddenFor(m => m.Questions[x].ExternalId)
@Html.HiddenFor(m => m.Questions[x].Text)
@Html.HiddenFor(m => m.Questions[x].Type)
@Html.HiddenFor(m => m.Questions[x].AskWhy)
@Html.HiddenFor(m => m.Questions[x].Mandatory)
@Html.HiddenFor(m => m.Questions[x].Origin)
@Html.HiddenFor(m => m.Questions[x].CommentText)
@Html.HiddenFor(m => m.Questions[x].BeenUsed)
@Html.HiddenFor(m => m.Questions[x].Included)
</div>
}
}
</body>
</html>
這將產生以下HTML:
<html>
<head>
<script id="dxis_1397937255" src="/DXR.axd?r=1_142-_ND58" type="text/javascript"></script>
<script id="dxis_29183559" src="/DXR.axd?r=1_80-_ND58" type="text/javascript"></script>
<script id="dxis_1801595362" src="/DXR.axd?r=1_135-_ND58" type="text/javascript"></script>
<script id="dxis_40984204" src="/DXR.axd?r=1_91-_ND58" type="text/javascript"></script>
<script id="dxis_1510326740" src="/DXR.axd?r=14_0-VND58" type="text/javascript"></script>
<script id="dxis_955216353" src="/DXR.axd?r=1_98-_ND58" type="text/javascript"></script>
<script id="dxis_1818762533" src="/DXR.axd?r=1_104-_ND58" type="text/javascript"></script>
<script id="dxis_1042080475" src="/DXR.axd?r=1_105-_ND58" type="text/javascript"></script>
<script id="dxis_2035868379" src="/DXR.axd?r=1_101-_ND58" type="text/javascript"></script>
<script id="dxis_237603203" src="/DXR.axd?r=1_84-_ND58" type="text/javascript"></script>
<script id="dxis_964747995" src="/DXR.axd?r=1_109-_ND58" type="text/javascript"></script>
<script id="dxis_787399442" src="/DXR.axd?r=14_23-VND58" type="text/javascript"></script>
<script id="dxis_1504896761" src="/DXR.axd?r=1_92-_ND58" type="text/javascript"></script>
<script id="dxis_1295258881" src="/DXR.axd?r=14_1-VND58" type="text/javascript"></script>
<script id="dxis_1524734997" src="/DXR.axd?r=1_77-_ND58" type="text/javascript"></script>
<script id="dxis_691625841" src="/DXR.axd?r=1_128-_ND58" type="text/javascript"></script>
<script id="dxis_799056015" src="/DXR.axd?r=1_126-_ND58" type="text/javascript"></script>
<script id="dxis_382503299" src="/DXR.axd?r=1_94-_ND58" type="text/javascript"></script>
<script id="dxis_1728027670" src="/DXR.axd?r=1_97-_ND58" type="text/javascript"></script>
<script id="dxis_879896160" src="/DXR.axd?r=1_95-_ND58" type="text/javascript"></script>
<script id="dxis_1869190171" src="/DXR.axd?r=1_96-_ND58" type="text/javascript"></script>
<script id="dxis_908190427" src="/DXR.axd?r=1_106-_ND58" type="text/javascript"></script>
<script id="dxis_1718746384" src="/DXR.axd?r=14_4-VND58" type="text/javascript"></script>
<script id="dxis_824974629" src="/DXR.axd?r=1_100-_ND58" type="text/javascript"></script>
<script id="dxis_350755308" src="/DXR.axd?r=1_117-_ND58" type="text/javascript"></script>
<script id="dxis_1538974427" src="/DXR.axd?r=1_103-_ND58" type="text/javascript"></script>
<script id="dxis_395477619" src="/DXR.axd?r=14_12-VND58" type="text/javascript"></script>
<script id="dxis_758481523" src="/DXR.axd?r=14_13-VND58" type="text/javascript"></script>
<script id="dxis_1901978331" src="/DXR.axd?r=1_102-_ND58" type="text/javascript"></script>
<script id="dxis_742498447" src="/DXR.axd?r=1_129-_ND58" type="text/javascript"></script>
<script id="dxis_691084581" src="/DXR.axd?r=1_107-_ND58" type="text/javascript"></script>
</head>
<body>
<form action="/Survey/Start/03926d2d-6bc4-4856-ba1d-9c751ea31529" method="post">
<input data-val="true" data-val-length="The field EmployeeSurveyName must be a string with a maximum length of 250." data-val-length-max="250" data-val-required=" * Required" id="EmployeeSurveyName" name="EmployeeSurveyName" type="hidden" value="" />
<input type="submit" />
<input id="Employees_0__Name" name="Employees[0].Name" type="hidden" value="" />
<input id="Employees_0__ExternalId" name="Employees[0].ExternalId" type="hidden" value="" />
<input id="Employees_0__Included" name="Employees[0].Included" type="hidden" value="" />
<input id="Employees_1__Name" name="Employees[1].Name" type="hidden" value="" />
<input id="Employees_1__ExternalId" name="Employees[1].ExternalId" type="hidden" value="" />
<input id="Employees_1__Included" name="Employees[1].Included" type="hidden" value="" />
<div>
<input type="hidden" name="E5C665C9-1EA5-4DD5-97C6-20A47813A61Fa" />
<input type="hidden" name="E5C665C9-1EA5-4DD5-97C6-20A47813A61Fb" />
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Questions_0__Id" name="Questions[0].Id" type="hidden" value="0" />
<input id="Questions_0__ExternalId" name="Questions[0].ExternalId" type="hidden" value="E5C665C9-1EA5-4DD5-97C6-20A47813A61F" />
<input data-val="true" data-val-length="The field Text must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_0__Text" name="Questions[0].Text" type="hidden" value="" />
<input data-val="true" data-val-required="The Type field is required." id="Questions_0__Type" name="Questions[0].Type" type="hidden" value="ZeroToTen" />
<input data-val="true" data-val-required="The AskWhy field is required." id="Questions_0__AskWhy" name="Questions[0].AskWhy" type="hidden" value="False" />
<input data-val="true" data-val-required="The Mandatory field is required." id="Questions_0__Mandatory" name="Questions[0].Mandatory" type="hidden" value="False" />
<input data-val="true" data-val-required="The Origin field is required." id="Questions_0__Origin" name="Questions[0].Origin" type="hidden" value="Template" />
<input data-val="true" data-val-length="The field CommentText must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_0__CommentText" name="Questions[0].CommentText" type="hidden" value="" />
<input data-val="true" data-val-required="The BeenUsed field is required." id="Questions_0__BeenUsed" name="Questions[0].BeenUsed" type="hidden" value="False" />
<input data-val="true" data-val-required="The Included field is required." id="Questions_0__Included" name="Questions[0].Included" type="hidden" value="False" />
</div>
<div>
<input type="hidden" name="F34DF428-AAC7-48F0-8236-670FD197B189a" />
<input type="hidden" name="F34DF428-AAC7-48F0-8236-670FD197B189b" />
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Questions_1__Id" name="Questions[1].Id" type="hidden" value="0" />
<input id="Questions_1__ExternalId" name="Questions[1].ExternalId" type="hidden" value="F34DF428-AAC7-48F0-8236-670FD197B189" />
<input data-val="true" data-val-length="The field Text must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_1__Text" name="Questions[1].Text" type="hidden" value="" />
<input data-val="true" data-val-required="The Type field is required." id="Questions_1__Type" name="Questions[1].Type" type="hidden" value="ZeroToTen" />
<input data-val="true" data-val-required="The AskWhy field is required." id="Questions_1__AskWhy" name="Questions[1].AskWhy" type="hidden" value="False" />
<input data-val="true" data-val-required="The Mandatory field is required." id="Questions_1__Mandatory" name="Questions[1].Mandatory" type="hidden" value="False" />
<input data-val="true" data-val-required="The Origin field is required." id="Questions_1__Origin" name="Questions[1].Origin" type="hidden" value="Template" />
<input data-val="true" data-val-length="The field CommentText must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_1__CommentText" name="Questions[1].CommentText" type="hidden" value="" />
<input data-val="true" data-val-required="The BeenUsed field is required." id="Questions_1__BeenUsed" name="Questions[1].BeenUsed" type="hidden" value="False" />
<input data-val="true" data-val-required="The Included field is required." id="Questions_1__Included" name="Questions[1].Included" type="hidden" value="False" />
</div>
<div>
<input type="hidden" name="ab75f0d9-2be9-4f04-baab-e3157651ac14a" />
<input type="hidden" name="ab75f0d9-2be9-4f04-baab-e3157651ac14b" />
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Questions_2__Id" name="Questions[2].Id" type="hidden" value="0" />
<input id="Questions_2__ExternalId" name="Questions[2].ExternalId" type="hidden" value="ab75f0d9-2be9-4f04-baab-e3157651ac14" />
<input data-val="true" data-val-length="The field Text must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_2__Text" name="Questions[2].Text" type="hidden" value="" />
<input data-val="true" data-val-required="The Type field is required." id="Questions_2__Type" name="Questions[2].Type" type="hidden" value="ZeroToTen" />
<input data-val="true" data-val-required="The AskWhy field is required." id="Questions_2__AskWhy" name="Questions[2].AskWhy" type="hidden" value="False" />
<input data-val="true" data-val-required="The Mandatory field is required." id="Questions_2__Mandatory" name="Questions[2].Mandatory" type="hidden" value="False" />
<input data-val="true" data-val-required="The Origin field is required." id="Questions_2__Origin" name="Questions[2].Origin" type="hidden" value="Template" />
<input data-val="true" data-val-length="The field CommentText must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_2__CommentText" name="Questions[2].CommentText" type="hidden" value="" />
<input data-val="true" data-val-required="The BeenUsed field is required." id="Questions_2__BeenUsed" name="Questions[2].BeenUsed" type="hidden" value="False" />
<input data-val="true" data-val-required="The Included field is required." id="Questions_2__Included" name="Questions[2].Included" type="hidden" value="False" />
</div>
<div>
<input type="hidden" name="9f98d45b-e988-4578-a8ab-625edb2c4bd9a" />
<input type="hidden" name="9f98d45b-e988-4578-a8ab-625edb2c4bd9b" />
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Questions_3__Id" name="Questions[3].Id" type="hidden" value="0" />
<input id="Questions_3__ExternalId" name="Questions[3].ExternalId" type="hidden" value="9f98d45b-e988-4578-a8ab-625edb2c4bd9" />
<input data-val="true" data-val-length="The field Text must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_3__Text" name="Questions[3].Text" type="hidden" value="" />
<input data-val="true" data-val-required="The Type field is required." id="Questions_3__Type" name="Questions[3].Type" type="hidden" value="ZeroToTen" />
<input data-val="true" data-val-required="The AskWhy field is required." id="Questions_3__AskWhy" name="Questions[3].AskWhy" type="hidden" value="False" />
<input data-val="true" data-val-required="The Mandatory field is required." id="Questions_3__Mandatory" name="Questions[3].Mandatory" type="hidden" value="False" />
<input data-val="true" data-val-required="The Origin field is required." id="Questions_3__Origin" name="Questions[3].Origin" type="hidden" value="Template" />
<input data-val="true" data-val-length="The field CommentText must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_3__CommentText" name="Questions[3].CommentText" type="hidden" value="" />
<input data-val="true" data-val-required="The BeenUsed field is required." id="Questions_3__BeenUsed" name="Questions[3].BeenUsed" type="hidden" value="False" />
<input data-val="true" data-val-required="The Included field is required." id="Questions_3__Included" name="Questions[3].Included" type="hidden" value="False" />
</div>
<div>
<input type="hidden" name="41321194-3EDB-4712-8548-0FFD7B732AC9a" />
<input type="hidden" name="41321194-3EDB-4712-8548-0FFD7B732AC9b" />
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Questions_4__Id" name="Questions[4].Id" type="hidden" value="0" />
<input id="Questions_4__ExternalId" name="Questions[4].ExternalId" type="hidden" value="41321194-3EDB-4712-8548-0FFD7B732AC9" />
<input data-val="true" data-val-length="The field Text must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_4__Text" name="Questions[4].Text" type="hidden" value="" />
<input data-val="true" data-val-required="The Type field is required." id="Questions_4__Type" name="Questions[4].Type" type="hidden" value="ZeroToTen" />
<input data-val="true" data-val-required="The AskWhy field is required." id="Questions_4__AskWhy" name="Questions[4].AskWhy" type="hidden" value="False" />
<input data-val="true" data-val-required="The Mandatory field is required." id="Questions_4__Mandatory" name="Questions[4].Mandatory" type="hidden" value="False" />
<input data-val="true" data-val-required="The Origin field is required." id="Questions_4__Origin" name="Questions[4].Origin" type="hidden" value="Template" />
<input data-val="true" data-val-length="The field CommentText must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_4__CommentText" name="Questions[4].CommentText" type="hidden" value="" />
<input data-val="true" data-val-required="The BeenUsed field is required." id="Questions_4__BeenUsed" name="Questions[4].BeenUsed" type="hidden" value="False" />
<input data-val="true" data-val-required="The Included field is required." id="Questions_4__Included" name="Questions[4].Included" type="hidden" value="False" />
</div>
<div>
<input type="hidden" name="F2DD07DB-B107-493B-A9DD-523CCD6DDD4Fa" />
<input type="hidden" name="F2DD07DB-B107-493B-A9DD-523CCD6DDD4Fb" />
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Questions_5__Id" name="Questions[5].Id" type="hidden" value="0" />
<input id="Questions_5__ExternalId" name="Questions[5].ExternalId" type="hidden" value="F2DD07DB-B107-493B-A9DD-523CCD6DDD4F" />
<input data-val="true" data-val-length="The field Text must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_5__Text" name="Questions[5].Text" type="hidden" value="" />
<input data-val="true" data-val-required="The Type field is required." id="Questions_5__Type" name="Questions[5].Type" type="hidden" value="ZeroToTen" />
<input data-val="true" data-val-required="The AskWhy field is required." id="Questions_5__AskWhy" name="Questions[5].AskWhy" type="hidden" value="False" />
<input data-val="true" data-val-required="The Mandatory field is required." id="Questions_5__Mandatory" name="Questions[5].Mandatory" type="hidden" value="False" />
<input data-val="true" data-val-required="The Origin field is required." id="Questions_5__Origin" name="Questions[5].Origin" type="hidden" value="Template" />
<input data-val="true" data-val-length="The field CommentText must be a string with a maximum length of 1000." data-val-length-max="1000" id="Questions_5__CommentText" name="Questions[5].CommentText" type="hidden" value="" />
<input data-val="true" data-val-required="The BeenUsed field is required." id="Questions_5__BeenUsed" name="Questions[5].BeenUsed" type="hidden" value="False" />
<input data-val="true" data-val-required="The Included field is required." id="Questions_5__Included" name="Questions[5].Included" type="hidden" value="False" />
</div>
</form>
</body>
</html>
當我提交這種形式下,員工屬性爲null,雖然它應該是一個包含2個項目的列表。但是,在它變得非常奇怪的是,所有正常工作的種種到HTML或潛在的剃刀改變這種輕微的變化:
- 如果我更改了Question_5用於不同的GUID的GUID,它的工作原理。
- 如果我刪除的隱藏字段的任何單獨一個,它的工作原理(儘管當然對應字段刪除了財產不會被綁定)。
- 如果我刪除問題2或問題6,它可以工作。但刪除任何其他單個問題沒有幫助!刪除任何兩個或更多的問題總是會修復它。
- 如果我刪除了DevExpress()。GetScripts,它的工作原理。
- 如果我更改了從
string.Format("{0}a", Model.Questions[x].ExternalId)
到string.Format("a{0}", Model.Questions[x].ExternalId)
第一隱藏輸入的名稱,它的工作原理。
基本上,它似乎的不受Html.HiddenFor創建的2個輸入()的名稱可以是潛在的原因,因爲改變了他們的名字不開始與GUID似乎解決它。但是,特定的GUID會如何導致這種情況呢?
我已經試過步入MVC模型綁定代碼;我無法找到它在工作時間和不工作時間之間的差異。我也重寫了BindProperty()並使用了一個自定義模型綁定器,這樣我可以查看BindingContext和PropertyDescriptors。在它工作的時間和不工作的時間之間,一切看起來都一樣。當它工作後,在「Employees」上調用BindProperty之後,它會在每個員工的每個屬性(Name,ExternalId,Included)上調用它。然後在我的視圖模型中調用我的setter作爲Employees列表。當它不起作用時,在員工上調用BindProperty之後,它只會移動到Questions上。
不管怎樣,我一直在努力,現在幾個小時所有這些不同的東西;我甚至無法想到一個可能的解釋,也許我的隨機生成的GUID恰好從一個特定的字符序列開始,當它與表單上的其他元素結合時,會導致某種與散列值的GUID衝突形式或者像這樣瘋狂的東西...
編輯我簡化了我的觀點,只是保持導致問題的最簡單的輸入:
@model eNPSWeb.Models.Survey_c.StartModel
@{
Layout = null;
}
<html>
<head>
<title>What the...</title>
</head>
<body>
@Html.DevExpress().GetScripts(new Script { ExtensionSuite = ExtensionSuite.Editors })
@using (Html.BeginForm())
{
<input type="hidden" name="EmployeeSurveyName" />
<input type="submit" />
<input name="Employees[0].Name" />
<input name="Employees[0].ExternalId" />
<input name="Employees[0].Included" />
<input name="Employees[1].Name" />
<input name="Employees[1].ExternalId" />
<input name="Employees[1].Included" />
<input name="a" />
<input name="b" />
<input name="c" />
<input name="d" />
<input name="e" />
<input name="f" />
<input name="g" />
<input name="h" />
<input name="i" />
<input name="2" />
<div>
<input type="hidden" name="Questions[0].Id" />
<input type="hidden" name="Questions[0].ExternalId" />
<input type="hidden" name="Questions[0].Text" />
<input type="hidden" name="Questions[0].Type" />
<input type="hidden" name="Questions[0].AskWhy" />
<input type="hidden" name="Questions[0].Mandatory" />
<input type="hidden" name="Questions[0].Origin" />
<input type="hidden" name="Questions[0].CommentText" />
<input type="hidden" name="Questions[0].BeenUsed" />
<input type="hidden" name="Questions[0].Included" />
</div>
<div>
<input type="hidden" name="Questions[1].Id" />
<input type="hidden" name="Questions[1].ExternalId" />
<input type="hidden" name="Questions[1].Text" />
<input type="hidden" name="Questions[1].Type" />
<input type="hidden" name="Questions[1].AskWhy" />
<input type="hidden" name="Questions[1].Mandatory" />
<input type="hidden" name="Questions[1].Origin" />
<input type="hidden" name="Questions[1].CommentText" />
<input type="hidden" name="Questions[1].BeenUsed" />
<input type="hidden" name="Questions[1].Included" />
</div>
<div>
<input type="hidden" name="Questions[2].Id" />
<input type="hidden" name="Questions[2].ExternalId" />
<input type="hidden" name="Questions[2].Text" />
<input type="hidden" name="Questions[2].Type" />
<input type="hidden" name="Questions[2].AskWhy" />
<input type="hidden" name="Questions[2].Mandatory" />
<input type="hidden" name="Questions[2].Origin" />
<input type="hidden" name="Questions[2].CommentText" />
<input type="hidden" name="Questions[2].BeenUsed" />
<input type="hidden" name="Questions[2].Included" />
</div>
<div>
<input type="hidden" name="Questions[3].Id" />
<input type="hidden" name="Questions[3].ExternalId" />
<input type="hidden" name="Questions[3].Text" />
<input type="hidden" name="Questions[3].Type" />
<input type="hidden" name="Questions[3].AskWhy" />
<input type="hidden" name="Questions[3].Mandatory" />
<input type="hidden" name="Questions[3].Origin" />
<input type="hidden" name="Questions[3].CommentText" />
<input type="hidden" name="Questions[3].BeenUsed" />
<input type="hidden" name="Questions[3].Included" />
</div>
<div>
<input type="hidden" name="Questions[4].Id" />
<input type="hidden" name="Questions[4].ExternalId" />
<input type="hidden" name="Questions[4].Text" />
<input type="hidden" name="Questions[4].Type" />
<input type="hidden" name="Questions[4].AskWhy" />
<input type="hidden" name="Questions[4].Mandatory" />
<input type="hidden" name="Questions[4].Origin" />
<input type="hidden" name="Questions[4].CommentText" />
<input type="hidden" name="Questions[4].BeenUsed" />
<input type="hidden" name="Questions[4].Included" />
</div>
}
</body>
</html>
當我混淆了輸入名稱a,b,c等;我變得非常令人驚訝的結果:
正常工作:
<input name="a" />
<input name="b" />
<input name="c" />
<input name="d" />
<input name="e" />
<input name="f" />
<input name="g" />
<input name="h" />
<input name="1" />
<input name="2" />
不工作(僱員爲null):
<input name="a" />
<input name="b" />
<input name="c" />
<input name="d" />
<input name="e" />
<input name="f" />
<input name="g" />
<input name="h" />
<input name="i" />
<input name="2" />
正常工作:
<input name="a" />
<input name="b" />
<input name="c" />
<input name="d" />
<input name="e" />
<input name="f" />
<input name="g" />
<input name="h" />
<input name="i" />
<input name="j" />
不工作:
<input name="b" />
<input name="c" />
<input name="d" />
<input name="e" />
<input name="f" />
<input name="g" />
<input name="h" />
<input name="i" />
<input name="2" />
<input name="3" />
就好像輸入名稱的非常特定的組合導致其中斷一樣。 (在完整頁面中,輸入名稱來自GUID)。