私の現在のプロジェクトの一環として、私は殆ど知られていない巨大なMVC Webフォームにぶち壊して、かなり深刻なデバッグをしなければなりません。 WebFormsの経験は豊富ですが、MVCはほとんどありませんので、次のことが明らかでない場合、または愚かな質問をしている場合は、私を許してください。ASP.net MVCモデルが値を認識できない
フォームの一部は、別のURLから情報を取得し、ユーザーがその情報に基づいてフォームのそのセクションに配置できるものを制限するカスタム.ascxコントロールです。すべてのフォーム要素をオン/オフできます。この特定のフォーム要素をオンにして、それを記入して提出すると、ModelStateはその検証チェックに失敗し、必要な値が失われていると不平を言ってしまいます。フォームは問題のある要素をオンにして返されますが、ボックスには値はありません。この特定の要素をオンにしないと、ModelStateは正しく検証します。
フォーム要素とモデルの間のマッピングがボンネットの下で行われることを理解するには、MVCについて十分に知っています。しかし、これはデバッグが非常に困難です。私は実際に問題が何であるか、さらに悪いことに、原因が何であるかを調べる方法を知らない。いずれかのフロントの提案は非常に高く評価されます。
EDIT:いくつかのコード
//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);
}
EDIT:BlackWhiteListConfigViewData.ascxコード
IF(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)%>
乾杯、 マット
フォーム要素がどこでどのようにオン/オフされているかを詳しく説明できますか? – Henry
チェックボックスに基づいています。フォーム要素は、デフォルトでtype = "hidden"です。ボックスがチェックされている場合、javascriptで削除されます。明らかに、このインスタンスでは、要素は表示されず、隠蔽されませんが、その中にテキストボックスは値を保持していません。 –
POST値が正しくバインドされていない可能性があります。 (1)特定のフォーム要素を生成するビューコード(2)バインドするプロパティ – Henry