在MVC表单上的两个位置更新相同的属性

时间:2011-12-09 18:30:20

标签: asp.net-mvc asp.net-mvc-3 forms updates

我在MVC3表单上有两个必须表示相同值的字段。如果更新了一个,它应该更新另一个并显示正确的值(Gotta love requirements)。由于可能是显而易见的原因,该字段(页面上的第二个字段)无法正确更新。有没有办法实现我的需要?

例如:

第一个例子

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

OR:

第二个例子

    <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'>&nbsp;123</span><span class='separator'>&nbsp;|&nbsp;</span>FBI<span class='value-display'>&nbsp;123</span><span class='separator'>&nbsp;|&nbsp;</span>SSN<span class='value-display'>&nbsp;222-22-2222</span><span class='separator'>&nbsp;|&nbsp;</span>ITN<span class='value-display'>&nbsp;123</span><span class='separator'>&nbsp;|&nbsp;</span>BCI<span class='value-display'>&nbsp;13</span><span class='separator'>&nbsp;|&nbsp;</span>ID<span class='value-display'>&nbsp;123</span><span class='separator'>&nbsp;|&nbsp;</span>DL<span class='value-display'>&nbsp;123</span><span class='separator'>&nbsp;|&nbsp;</span></span></li>

</ol>

此HTML(这是示例2中的html)与第一个示例中生成的HTML之间的唯一区别是两个控件具有相同的ID和名称。以下是在调用submit时由BeginRouteForm html帮助程序调用的控制器方法中发生的情况:

公共ActionResult更新(int partyId,FormCollection数据)     {       var party = Party.Find(partyId).OrNotFound();       var vm = new PartyEditViewModel(party.Case,party);

  try
  {
    UpdateModel(vm, data);

    return JsView("Update.js", party);
  }
  catch (UpdateModelException)
  {
    return JsView("Edit.js", vm);
  }
}

1 个答案:

答案 0 :(得分:1)

好吧看起来导致问题的原因是你对Html.TextFieldFor的调用可能会创建两个具有相同名称的控件和绑定到单独模型属性的Id。如果是这种情况,您需要正确地为控件的id和名称添加前缀,以便在模型绑定启动时,它将正确地将值绑定到模型的属性。