views:

110

answers:

2

I have a section of my site where users can add addresses to their account. They may add as many as they need (shipping, billing, etc).

I set things up so that after an address is added, the users sees the address in an update form with a "save" and "delete" button. The user can adjust any of the addresses they have added.

The problem I am having is with validation. Let's say Line 1 is required. If I am updating the second of three addresses and leave Line 1 empty the controller is raising an error (using the same technique from Nerd Dinner, BTW). This is good. What is bad is that all of the address info on all of the addresses listed in the view, now show as values from the address where the error was raised.

I know this has something to do with model binding, but I am confused, as the form data is set up as follows:

<%= Html.TextBox("Line1", Model.Address.Line1)%>

The Model that is passed in is unique to the address we are on in the list of client addresses. I am not sure why the value in "Model.Addres.Line1" is being overridden by data in the ModelState ModelErrors Collection. I guess the default behavior is to use the values from the errors collection when they are present. This is a problem when there is more than one form on the View and the form is using the same Names for input fields as each of the other forms.

Is my only work-around to avoind the Html Helper function here and hard-code the inputs in HTML?

A: 

Correct me if you're wrong, but it sounds like all 3 addresses will have the same field names in the same form? If so, you've got bigger problems than just the validation message.

Regardless (even if you're using separate forms), I would suggest you preface the field names with the address ID:

<%= Html.TextBox("Address" + Model.Address.ID + ".Line1", Model.Address.Line1)%>

You won't be able to automatically bind (in the method parameters), but you can use UpdateModel() and set the prefix to

"Address" + Address.ID
James S
There is a separate form for each of the addresses. If I preface the fieldname with the unique ID then I lose the model binding I want for the updates in the controller. BTW, when there are no validation errors the updates are successful. I am only running into issues when errors occur.
mikerennick
Yes, you do lose the automatic model binding. If you want to keep that, you'd have to have three model bindings (2 of which will always be null) -- there's a prefix option for the bindings, as well.Alternatively, you could do two things:a) Only show the validationMessage() for the submitted form. Don't call validationMessage() for the addresses which haven't been submitted. The view will be a bit messy, though.b) Keep the Html.TextBox() the same, but add the prefixes to only the ValidationMessage(). Do something in your controller to rename the validation errors in viewstate. Also messy.
James S
A: 

I solved this by getting rid of the html helper methods on the form(s). If someone sees a means of keeping the helper methods please let me know.

mikerennick
Just saw that the post above was updated with a solution. I was not aware of the prefix option on the UpdateModel method. Thanks.
mikerennick