gpt4 book ai didi

c# - MVC 嵌套模型绑定(bind)只能部分工作——我错过了什么?

转载 作者:行者123 更新时间:2023-11-30 13:39:33 27 4
gpt4 key购买 nike

我有一个名为 AssetSearchModel 的 ViewModel,其属性类型为:List。

当我从 ViewModel 发布时,我的列表的所有属性都成功绑定(bind)回模型,除了这个:public DomainsDto DomainControl { get;放; 定义在底部。

那个域控件有几个属性,然后是它自己的嵌套列表......它的任何属性都没有设置,它们都是空的

public class AssetSearchModel
{
public List<SearchControlModel> SearchControls { get; set; }

public AssetSearchModel()
{
}

}

public class SearchControlModel
{

/// <summary>
/// Asset identifier.
/// </summary>
[DisplayName("AssetId ID")]
public int SearchOptionID { get; set; } // this gets bound ok

[DisplayName("Asset Customer ID")]
public int AssetCustomerID { get; set; } // this gets bound ok

[DisplayName("Portal ID")]
public int PortalID { get; set; } // this gets bound ok

[DisplayName("Calling Page")]
public string CallingPage { get; set; } // this gets bound ok

[DisplayName("Display Control")]
public string DisplayControl { get; set; } // this gets bound ok

[DisplayName("Display Text")]
public string DisplayText { get; set; } // this gets bound ok

public string ControlValue { get; set; } // this gets bound ok

[DisplayName("Search Table")]
public string SearchTable { get; set; } // this gets bound ok

[DisplayName("Search Column")]
public string SearchColumn { get; set; } // this gets bound ok

[DisplayName("Source Table")]
public string SourceTable { get; set; } // this gets bound ok

[DisplayName("Source Display Column")]
public string SourceDisplayColumn { get; set; } // this gets bound ok

[DisplayName("Source Value Column")]
public string SourceValueColumn { get; set; } // this gets bound ok

[DisplayName("SourceCustomFilter")]
public string SourceCustomFilter { get; set; } // this gets bound ok

[DisplayName("SortOrder")]
public int SortOrder { get; set; } // this gets bound ok

[DisplayName("DomainControl")]
public DomainsDto DomainControl { get; set; } // THIS DOES NOT BIND

public SearchControlModel()
{



}

}

public class DomainsDto
{
public int DomainMasterId; // this does not bind
public string DisplayName; // this does not bind
public List<DomainDto> Domains; // this does not bind
public DomainsDto()
{

}

}

[DataContract(Name = "Domain", Namespace = "http://www.comcom.com/")]

public class DomainDto
{
[DataMember]
public int DomainID { get; set; } // this does not bind

[DataMember]
public string DomainName { get; set; } // this does not bind

[DataMember]
public bool Checked { get; set; } // this does not bind


public DomainDto()
{

}
}

我的观点:

         @for (int y = 0; y < Model.SearchControls.Count; y++)
{

@Html.Label("Search option: " + Model.SearchControls[y].DisplayText)
<br />
@Html.HiddenFor(x => x.SearchControls[y].DisplayText)
@Html.HiddenFor(x => x.SearchControls[y].CallingPage)
@Html.HiddenFor(x => x.SearchControls[y].DisplayControl)
@Html.HiddenFor(x => x.SearchControls[y].SourceCustomFilter)
@Html.HiddenFor(x => x.SearchControls[y].SearchTable)
@Html.HiddenFor(x => x.SearchControls[y].SearchColumn)
@Html.HiddenFor(x => x.SearchControls[y].SourceValueColumn)


if (Model.SearchControls[y].DomainControl != null)
{

@Html.HiddenFor(x => x.SearchControls[y].DomainControl.DisplayName)

@Html.HiddenFor(x => x.SearchControls[y].DomainControl.DomainMasterId)

if (Model.SearchControls[y].DomainControl.Domains != null)
{
for (int z = 0; z < Model.SearchControls[y].DomainControl.Domains.Count; z++)
{

@Html.HiddenFor(x => x.SearchControls[y].DomainControl.Domains[z].DomainID)
@Html.HiddenFor(x => x.SearchControls[y].DomainControl.Domains[z].DomainName)
@Html.HiddenFor(x => x.SearchControls[y].DomainControl.Domains[z].Checked)


<br />
}
}
}

<br />
<br />
<hr width=100px />


}

html(抱歉单行,它不会让我以不同的方式粘贴它)

<label for="Search_option:_Product_Lines">Search option: Product Lines</label> <input id="SearchControls_1__DisplayText" name="SearchControls[1].DisplayText" type="hidden" value="Product Lines" /> <input id="SearchControls_1__CallingPage" name="SearchControls[1].CallingPage" type="hidden" value="AssetSearch" /> <input id="SearchControls_1__DisplayControl" name="SearchControls[1].DisplayControl" type="hidden" value="M" /> <input id="SearchControls_1__SourceCustomFilter" name="SearchControls[1].SourceCustomFilter" type="hidden" value="26" /> <input id="SearchControls_1__SearchTable" name="SearchControls[1].SearchTable" type="hidden" value="AssetDomainValue" /> <input id="SearchControls_1__SearchColumn" name="SearchControls[1].SearchColumn" type="hidden" value="Brand" /> <input id="SearchControls_1__SourceValueColumn" name="SearchControls[1].SourceValueColumn" type="hidden" value="DomainCodeValueID" />   <input id="SearchControls_1__DomainControl_DisplayName" name="SearchControls[1].DomainControl.DisplayName" type="hidden" value="Product Line" /> <input id="SearchControls_1__DomainControl_DomainMasterId" name="SearchControls[1].DomainControl.DomainMasterId" type="hidden" value="26" />  <input id="SearchControls_1__DomainControl_Domains_0__DomainID" name="SearchControls[1].DomainControl.Domains[0].DomainID" type="hidden" value="43680" /> <input id="SearchControls_1__DomainControl_Domains_0__DomainName" name="SearchControls[1].DomainControl.Domains[0].DomainName" type="hidden" value="YORK" /> <input id="SearchControls_1__DomainControl_Domains_0__Checked" name="SearchControls[1].DomainControl.Domains[0].Checked" type="hidden" value="False" />                        <input id="SearchControls_1__DomainControl_Domains_1__DomainID" name="SearchControls[1].DomainControl.Domains[1].DomainID" type="hidden" value="43683" /> <input id="SearchControls_1__DomainControl_Domains_1__DomainName" name="SearchControls[1].DomainControl.Domains[1].DomainName" type="hidden" value="Johnson Controls" /> <input id="SearchControls_1__DomainControl_Domains_1__Checked" name="SearchControls[1].DomainControl.Domains[1].Checked" type="hidden" value="False" />                        <br />

最佳答案

啊不,模型 Binder 只适用于 properties只有,不是fields .

所以替换:

public class DomainsDto
{
public int DomainMasterId; // this does not bind
public string DisplayName; // this does not bind
public List<DomainDto> Domains; // this does not bind
public DomainsDto()
{

}
}

与:

public class DomainsDto
{
public int DomainMasterId { get; set; } // this does bind
public string DisplayName { get; set; } // this does bind
public List<DomainDto> Domains { get; set; } // this does bind
public DomainsDto()
{

}
}

但实际上,在表单中填充大量隐藏字段的意义何在?您的数据库中已经有这些东西了。因此,只需放置一个包含 id 的隐藏字段,即可让您前往数据所在的任何位置获取数据。我一直想知道人们为什么如此拼命地尝试为 MVC 重新发明 ViewState。这不是这种模式的使用方式。您不再使用经典的 WebForms。

在设计 ASP.NET MVC View 时应遵循以下规则:

  • 用户应该修改的任何字段都使用相应的输入字段(文本框、 radio 、ddls 和其他东西)
  • 用户不应修改的任何字段都不应出现在表单中。每个表单最多应该有一个隐藏字段,其中最多包含一个唯一标识符,以便您检索所需的信息

结论:使用 View 模型

因此,真正解决您问题的方法是替换:

[DisplayName("DomainControl")]
public DomainsDto DomainControl { get; set; } // THIS DOES NOT BIND

与:

public int DomainControlId { get; set; }

并在 View 中为这个 id 使用一个隐藏字段。就是这样。

关于c# - MVC 嵌套模型绑定(bind)只能部分工作——我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11585499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com