xVal and Validating multiple rows of data
I have a table name Discount that has the following schema:
PK DiscountID int
FK CustomerID int
Amount money
Name varchar(50)
So I am displaying all the discounts related to the customer. Each customer will have 3 discount records.
When I generate the form the ID's and Name's of the associated textboxes for editing that have to be unique to process correctly.
Example
When I try to validate using xVal, since my fie开发者_运维问答ld names are not matching the schema name, 'Amount_1' instead of 'Amount', it doesn't validate the field.
How can I get this to work ?
I cant combine all 3 discounts into one record for the unique customer, since there is some other fields i left out for simplifying the example. I need to have 3 discount's for each customer in 3 rows.
Here's some code:
<form method="post" action="ProcessUpdate">
<table>
<tr> <td> Discount 1 </td> <td> <%= Html.TextBox("Amount_1") %></td></tr>
<tr> <td> Discount 2 </td> <td> <%= Html.TextBox("Amount_2") %></td></tr>
<tr> <td> Discount 3 </td> <td> <%= Html.TextBox("Amount_3") %></td></tr>
<tr> <td> <input type="submit" value="submit"/> </td> </tr>
</table>
</form>
<%= Html.ClientSideValidation<Discount>() %>
Here my metadata
[MetadataType(typeof(DiscountMetaData))]
public partial class Discount
{
public class DiscountMetaData
{
[Required(ErrorMessage = " [Required] ")]
public string Amount { get; set; }
}
}
Any ideas on how to get that to work ?
I've played a bit with prefixes and this is how this could be done:
First you would have to name textboxes to have deferent prefix, but same property name "Amount" and then to attach jquery validators to each field by calling Html.ClientSideValidation three times, for each prefix.
<form method="post" action="ProcessUpdate">
<table>
<tr> <td> Discount 1 </td> <td> <%= Html.TextBox("discount1.Amount") %></td></tr>
<tr> <td> Discount 2 </td> <td> <%= Html.TextBox("discount2.Amount") %></td></tr>
<tr> <td> Discount 3 </td> <td> <%= Html.TextBox("discount3.Amount") %></td></tr>
<tr> <td> <input type="submit" value="submit"/> </td> </tr>
</table>
</form>
<%= Html.ClientSideValidation<Discount>("discount1") %>
<%= Html.ClientSideValidation<Discount>("discount2") %>
<%= Html.ClientSideValidation<Discount>("discount3") %>
Hope this helps
精彩评论