0

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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Bryan
  • 3,421
  • 8
  • 37
  • 77
  • what is value in span – shady youssery Aug 31 '16 at 12:29
  • Set the value of the property you want to bind to in the controller, not the view (your generating `name="Detail"` because that't the name of the property your accessing in the `HiddenFor()` method) –  Aug 31 '16 at 12:29
  • @StephenMuecke: I want to do this in the view. Is this possible? – Bryan Aug 31 '16 at 12:31
  • 2
    Why? (it belongs in the controller not the view) _Is it possible?_ - only with some awful hacks. What is the name of the property your wanting to bind to? –  Aug 31 '16 at 12:36
  • @StephenMuecke: LicenseHolder.LegalPerson.ContactDetails.Detail. But I can only access the Detail-property with the First-method as you can see. – Bryan Aug 31 '16 at 12:39
  • The binding isn't clever enough to understand the linq statement. **All** logic should be in the controller – Liam Aug 31 '16 at 12:43
  • Make me ill, but `@{ var detail = Model.LicenseHolder.LegalPerson ......Name == "BillingPhone").Detail; }` and `@Html.Hidden(" LicenseHolder.LegalPerson.ContactDetails.Detail", detail)` –  Aug 31 '16 at 12:44
  • @StephenMuecke: Ok, but If I do the binding In the controller, what should the parameters be that the controller should accept? controllername(string Detail, string value1, string value2 ...)? I think that Is awful, thats why I just use controllername(LicenseHolderDetailViewModel model) – Bryan Aug 31 '16 at 12:50
  • You just bind to your model (as your currently doing) - But you have referred to it as LicenseHolderDetail**ViewModel** but everything in your previous comments suggests that is not a view model at all. A view model does not contain data models, And a view model should be a flat structure (within reason). And a view model contains only values that you need in a view. And your ceratinly never use linq queries in a view if you use a view model. Recommend you read [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Aug 31 '16 at 12:56
  • My view model contains DTO-models. – Bryan Aug 31 '16 at 12:59
  • It should not contain data models! –  Aug 31 '16 at 13:11

1 Answers1

0

Ideally this logic should be in controller as Stephen Muecke suggested, but if that is not possible you may try using a for-loop:

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;
   }
}
Dhananjaya Kuppu
  • 1,322
  • 9
  • 10