tags:

views:

44

answers:

1

I have a strongly typed partial view named "Adress". This partial view contains 'input' fields associated to an Adress model class. I want to render it twice in the same view because the user must input the business adress and the delivery adress.

I have problem with client validation because of the ID of the 'input' rendered is not unique.

Also, I used the DataAnnotationModelBinder to get the model back on a httppost. I can't get the two adress object because of conflicting names in the Request.Forms.

Is there a way to set a kind of scope so the id of the 'input' for each partial view has a unique ID that can be recognized by the modelBinder?

A: 

I am not shure about the validation problem and data annotation, but usually you can work with muliple partials like this:

  • build a Viewmodel that aggregates the 2 Addresses. Lets say it's AddressViewData that has public properties BusinessAddress and DeliveryAddress. Both are of type Address. Address has an additional property "InstanceName". You fill the InstanceName with "BusinessAddress" and "DeliveryAddress"

  • the in the view call RenderPartial("Address",Model.BusinessAddress ) and RenderPartial("Address",Model.DeliveryAddress)

  • in the Partials use someting like this

    Html.TextBox( Model.InstanceName + ".Street" )

The result is a Formelement that has a name attribute like this "BusinessAddress.Street" The DefaultModelbinder will bind this correctly. Your action should use the Parameter

public ViewResult CreateAddress(AddressViewData  addresData)
Malcolm Frexner