开发者

RIA Services Decimal Required Field not causing validation error

I'm using Silverlight 4 with RIA services. I've created the domain service on the web app using EDMX. For one Entity table I have a decimal field called UnitPrice and annotated in the metadata as follows:

[Required(ErrorMessage="Price is required.",AllowEmptyStrings = false)]
[Range(typeof(decimal), "1", "1000",
      ErrorMessage = "Price must be between {1} and {2}")]
public decimal UnitPrice { get; set; }

On my client I have a page for inserting new records and the field is bound as follows:

<TextBox  Grid.Column="1" Grid.Row="4" Name="txtPrice" 
          Text="{Binding Path=UnitPrice, Mode=TwoWay, NotifyOnValidationError=true,
          ValidatesOnExceptions=True, ValidatesOnDataErrors=True,
          TargetNullValue='Price', StringFormat=\{0:c\}}" />

In the save button on the page I have the following:

if (newItem.HasValidationErrors)
{
    MessageBox.Show("please fix errors");
}
else
{
    try
    {
        db.SubmitChanges();
        EditWindow.Close();
    }
    catch (Exception ex)
    {

    }
}

When I go to add a new record, if I enter a value for the unit price as something out of the Range such as -10 I get the proper validation message and the domain context does not try to update the database.

My problem is that if I don't enter any value for Unitprice no validation appears and the data context tries to update the database and causes an unhanded exception error

Can someone suggest a way to cause the validation message to appear as it does w开发者_如何转开发ith the Range validation or do I need to manually check to see if the UnitPrice field is correct and display a message box if an error arises?

Thanks

Mark


I think this problem has to do with this property of the binding TargetNullValue='Price'. This specifies that the value 'Price' is used if the source is null. Try removing it to see if that solves the problem.

Hope this helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜