-1

Could you help me with the problem? Assume I had a form with the table of some stuff (products for example), with the possibility of add New product and Edit existing product. The requirement is to have add row is always be visible. Than i have a problem when, I click on the 'Edit' product, so that I have two inputs( one for edit and one for add ). When I posted the form to the server by choosing Save editable row, i wanted to validate only edit row and to do nothing with Add row (which is empty for a while).

And the problem is that default MVC validation is applied on the my Add row inputs, its controls become red, and property validation message appear. I don't want to see them, because it is really confusing. Actually i can't understand why this happens. Possibly because MVC can not cast empty string to DECIMAL (the type in the VIEWMODEL to which input is refered)

Alex Klunniy
  • 125
  • 1
  • 8
  • Could we have a bit of code please ? The model, the controller Edit action and the view would be nice. But out of the blue try with [this](http://stackoverflow.com/a/10896355/2248651) if the problem happens sever side – Simon Rapilly Aug 10 '13 at 13:47
  • You can: - Use JScript (JQuery) to fulfill the add text box with the existing info (so add became edit). Then the default validation will still be valid. - Disable default validation and implement custom JS to validate on browser side. Either way, remember to also validate on Server Side (controller). – Erre Efe Aug 10 '13 at 13:47
  • http://stackoverflow.com/help/how-to-ask – Paritosh Aug 10 '13 at 14:06
  • It's very difficult to understand what you want to achieve. Please post some code. – ataravati Aug 10 '13 at 15:43

1 Answers1

1

You need two different forms, one for Add Project and one for Edit Project. These need to point to different actions, which will then only validate one object.

Im not sure what your code looks like but I would really suggest something more like the following, where your views are separated entirely:

Start with a view that displays all of your 'Products' or whatever your data is. When you populate this in your view, you can then assign Edit links to each individual one. You only need one overall Add Project link. Like so:

@Html.ActionLink("Add Project", "AddProjectView")
For Each project In Model.Projects
    @Html.ActionLink("Edit", "EditView", New With {.id = project.ProjectID})
    @<div>PropertyName: @Project.Property</div>
Next

Now you can have an Edit view separated from your data view, which can handle validation all on its own. Likewise, the Add Project view is separated and handles its own validation. Upon successful Add or Edit, you can simply redirect the user back to your above view, where your data is displayed and updated.

ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64