I have a very simple Model with an ID(int) and 4 Non-Null String fields. They can be empty though. If I put [Required] on them it forces a Value to be entered. If I don't do anything and you leave it blank it throws an error as by default Blank==null.
How do I get it to default to String.Empty?
Related
I am doing a validation task for my Web API project. It exposes APIs to the front-end. The front-end should input valid values in the json file, but sometimes, if the invalid values coming it cause exceptions in my project.
For example, someId cannot be 0 or other invalid values. Because if 0 was set to someId, the DB will yell the foreign key constraints error.
My solution to this is using the attribute validation
[Range(1, 10, ErrorMessage = "{0} Values must be in range {1} to {2}")]
public int? someId { get; set; }
So it will prevent the invalid values from UI. But the little hassle is I need to hard code the valid data range in each fields. So I am asking if it has a better solution than mine?
I have a small question about EntityFramework Data Annotations (Code First).
I want to make integer / decimal required.
[Required]
public int? Nummer { get; set; }
But I have a small problem with this. The integer can't be null in my WPF application. Look at the screenshot bellow:
Because of the required the property wont change to null when the textbox is empty.
It's clearly visible the Selected row still has a number, 3 while it should be empty...
I don't have this problems with 'required' strings.
Why is this a problem? Because now the Opslaan (save) button doesn't get disabled when the number is 'empty'.
I can fix this by doing my data validation again manually.
With a switch and the IDataErrorInfo implementation.
Does someone know if I can solve this with the aid of the Data Annotations?
You could update your binding of your text box like this:
<TextBox Text="{Binding Nummer, TargetNullValue=''}"/>
With this binding the Nummer property gets set to null when the value of the text box is an empty string. If you don't specify TargetNullValue, the empty string cannot be converted to an int? and therefore you get a conversion error.
Just a question to get you right: When you want your Nummer to be required, why do you use int? as the data type instead of int?
I have a inputbox (text) that contains a datetime value, for some reason this input box has validation when I don't even require it.
My Model and ViewModel has no validation on the properites at all.
In my Razor view page I do have some input fields that have:
#Html.ValidationMessageFor(vm => vm.User.Age)
On this input I am trying to enter some string and it gives me a client-side validation warning:
#Html.TextBoxFor(vm => vm.User.BannedDate)
Below is the HTML that is rendered below the input box:
<input data-val="true" data-val-date="The field BannedDate must be a date." id="User_BannedDate" name="User.BannedDate" type="text" value="27/02/2014 12:00:00 AM" class="input-validation-error">
I am using Bootstrap, I have jquery also. Could it be detecting things automatically?
Update
Don't have a globalization tag in web.config
In my Controller's OnActionExecuting I am setting:
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-CA");
The framework is automatically generating these because it will try and bind anything you put in that text field to the backing property. Since BannedDate is a date and cannot accept values like Tomorrow, the framework is ensuring a valid date is submitted from the client. Looking at your output, it is not requiring those fields, only if something is entered into that field, it shall be a date.
As for the Age property, if you are not using a nullable int, you are seeing one of the following:
1) Automatically populates as a 0
2) If you are using a nullable int, it may be the same as BannedDate, in that you have to enter an integer and not 32 and a half! as an acceptable value.
If these automatic validations were not in place or if javascript was turned off on the client, you would receive a ModelState error during binding letting you know the value was not an acceptable value for those particular fields.
i have a column in database that takes a bit value(1,0). the default is set to 1. I am using Linq. when I try to change the value it gives me this exception.
A member that is computed or generated cannot be changed.
in Linq
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsDefault", DbType="Bit NOT NULL",IsDbGenerated="true" )]
if I remove the IsDbGenerated Attribute. I am able to change the values but when I import some data directly using a CSV file then the default value is always coming 0 but it was set to 1 in the DB.
Can't you set the default value of the field or property in code to 1 (or true) as well? You could also set the default in the constructor of the object that the field belongs to.
I have this enum:
public enum SupportedISOCurrencySymbol { DKK = 208, EUR = 978, NOK = 578, SEK = 752 }
I save the value for an order in the approved_orders table, Currency field.
I populate the sql insert query with a parameter like:
cmd.Parameters.AddWithValue("?Currency", this.Currency);
Well, If i do debug on the above line I clearly see that this.Currency has value DKK.
Why then it inserts in the DB: 208?
Do you have any ideea?
Thanks.
SupportedISOCurrencySymbol is an enum, which has integer as base. "DKK" represents the number 208.
What you are seeing in the debug window, is the value of .ToString() on your enum, which is defined to return the name of the enum, not the value. If you want to send the string "DKK" to your database, you could simply state it explicitly:
cmd.Parameters.AddWithValue("?Currency", this.Currency.ToString());
Of course, the columns involved in the database would then need to be of a compatible text type.
"DKK" is a friendly name for the value "208" for you (as programmer) to use in your code.
When the code is compiled all occurrences of "DKK" are replaced by "208" which will include the code that inserts the value into the database.
You could change your database schema to hold a string rather than an integer and store the name of the enum rather than it's value - but you would need to parse the string into an enum when reading it back out of the database into your code.
do you need to add a this.Currency.ToString()? so as to get the text not the numeric value of the enum?