在MVC3表单中有两个字段必须表示相同的值。如果一个更新了,它应该更新另一个并显示正确的值(必须喜欢的要求)。由于显而易见的原因,字段(页面上的第二个字段)没有正确更新。有没有办法达到我的目的?
例如:第一个例子 <li class="identifier-controls" @Model.IdentifierConfigurator.GenerateDataAttributes("ssn")>
@Html.TextFieldFor("Social Security Number", m => m.SocialSecurityNumber).SSN()
</li>
may appear twice on that form (In two different spots). (At this point, both of the controls have the same ID and name)
或:
第二个例子
<li class="identifier-controls" @Model.IdentifierConfigurator.GenerateDataAttributes("ssn")>
@Html.TextFieldFor("Social Security Number", m => m.SocialSecurityNumber).SSN()
</li>
和
<li class="identifier-controls" @Model.IdentifierConfigurator.GenerateDataAttributes("ssn")>
@Html.TextFieldFor("Social Security Number", m => m.Person.Description.SSN).SSN()
</li>
将出现在表单上。此时,输入的id是不同的,但它只会更新其中的一个。
下面是生成的HTML:<ol class="field-list display">
<li id="party-name"><span class="label">Name</span><span id="FormalDisplayName" class="value" style="width:140px">Guy, Bad</span></li>
<li><span class="label">Aliases</span><span id="AliasNames" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Status</span><span id="PartyStatusName" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Gender</span><span id="Description_Gender" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Juvenile</span><span id="Description_IsJuvenile" class="value" style="width:140px">No</span></li>
<li><span class="label">Ethnicity</span><span id="Description_Ethnicity" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Residential Status</span><span id="Description_ResidentialStatus" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Marital Status</span><span id="Description_MaritalStatus" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Religion</span><span id="Description_Religion" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Citizenship</span><span id="Description_Citizenship" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Birth City</span><span id="Description_BirthCity" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Date of Birth</span><span id="Description_BirthDate" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Date of Death</span><span id="Description_DeathDate" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Est. Age at Incident</span><span id="Description_EstimatedAgeAtIncident" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Adoption Date</span><span id="Description_AdoptionDate" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Multiple Birth</span><span id="Description_MultipleBirthName" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Email</span><span id="Description_Email" class="notentered" style="width:140px">Not entered</span></li>
<li><span class="label">Identifiers</span><span id="SuspectIdentifiersDisplay" class="value" style="width:400px">Prof<span class='value-display'> 123</span><span class='separator'> | </span>FBI<span class='value-display'> 123</span><span class='separator'> | </span>SSN<span class='value-display'> 222-22-2222</span><span class='separator'> | </span>ITN<span class='value-display'> 123</span><span class='separator'> | </span>BCI<span class='value-display'> 13</span><span class='separator'> | </span>ID<span class='value-display'> 123</span><span class='separator'> | </span>DL<span class='value-display'> 123</span><span class='separator'> | </span></span></li>
</ol>
这个HTML(这是来自示例2的HTML)和第一个示例中生成的HTML之间的唯一区别是这两个控件具有相同的ID和名称。下面是当submit被调用时,由BeginRouteForm html helper调用的控制器方法中发生的情况:
public ActionResult Update(int partyId, FormCollection data){var party = party . find (partyId).OrNotFound();var vm = new PartyEditViewModel(party. view)情况下,党);
try
{
UpdateModel(vm, data);
return JsView("Update.js", party);
}
catch (UpdateModelException)
{
return JsView("Edit.js", vm);
}
}
好的,看起来是什么导致的问题是你的Html调用。TextFieldFor可能正在创建两个具有相同名称和Id的控件,它们绑定到不同的模型属性。如果是这种情况,您需要正确地为控件的id和名称加上前缀,以便在模型绑定开始时,它将根据模型的属性正确地绑定值。