Make a hidden input in razor model

时间:2016-08-31 12:24:27

标签: asp.net asp.net-mvc razor

I have the following code:

<tr>
    <td class="tdstyle">
        <i class="fa fa-phone iconPos" aria-hidden="true"></i>
        <span>@Model.LicenseHolder.LegalPerson.ContactDetails.First(x => x.ContactDataType.Name == "BillingPhone").Detail</span>
        @Html.HiddenFor(x => x.LicenseHolder.LegalPerson.ContactDetails.First(y => y.ContactDataType.Name == "BillingPhone").Detail)
    </td>
</tr>

The generated input-field from above code Is this:

<input id="Detail" name="Detail" type="hidden" value="0730730037" />

This Is incorrect because It Is not binded to the Model. How can I generate a correct hidden input of this so the binding works?

1 个答案:

答案 0 :(得分:0)

理想情况下,这个逻辑应该在Stephen Muecke建议的控制器中,但如果不可能,你可以尝试使用for循环:

for (int i = 0; i < x.LicenseHolder.LegalPerson.ContactDetails.Length; i++){
     if (x.LicenseHolder.LegalPerson.ContactDetails[i].ContactDataType.Name == "BillingPhone") {
       @Html.HiddenFor(x => x.LicenseHolder.LegalPerson.ContactDetails[i].Detail)
      break;
   }
}