MVC3 Data Annotation Dataformat issue - asp.net-mvc-3

I have an MVC2 C# .Net web app. We are wanting one of our properties to always display with two decimal places: i.e.
.1 display as 0.10
1 display as 1.00
1.1 display as 1.10
1.21 display as 1.21
I have applied the following Data Annotations
[Display(Name = "Complexity/Scaling Factor")]
[DefaultValue(1.00)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0.00}")]
[Required(ErrorMessage = "Please enter a numeric value with up to two decimal places greater than zero.")]
[RegularExpression(#"^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$",
ErrorMessage = "Please enter a numeric value with up to two decimal places greater than zero.")]
public virtual double ComplexityFactor { get; set; }
...however the resulting properties are not displaying correctly.
2.1 displays as 2.1
1 displays as 1
Ideas?

Related

Why kendo mvc grid numeric integer column(numeric text box) is becoming zero , when I give decimal values to it

I am having kendo mvc grid and one of its column is numeric text box and it is of type integer , but when I try to give decimal values to numeric text box ,it is becoming zero , but it should get rounded to nearest integer or should not accept decimal values.
How can i do that , kindly anyone help me
CODE:
columns.Bound(c => c.Quantity).Title("Quantity").Width(100);
public int Quantity { get; set; }

Change a field to YesNo in Web Forms

I need to create a field in Web forms as a Yes/No field. However, on the web page it only takes values as 1,-1 or 0. How can i change this
Below is the code i am using and i want the StateID field to be a Yes/No field and the Card number field that can take a 16 digit value
Dim sSelectSQL As String = "CREATE TABLE Guests"
sSelectSQL &= "([GuestID] Number, [LName] TEXT(20),"
sSelectSQL &= "[FName] TEXT(20), [ZipCode] Number,"
sSelectSQL &= "[StateID] YesNo, [CardNo] Number)
In MS SQL there is no YE/NO field You need to set it a Bit (1 or 0)... Since i am new i am not able to comment, so just posting it as answer..
in dropdown you can replsent it as YES/NO or in actual 1 0r 0 replsents TRUE/FALSE state

Get number field in sitecore index

I'd like to know how do you guys get a value from a number field through index search.
I'm using WebForms for my project currently I have this model
[IndexField("SomeNumber")]
int SomeNumber { get; set; }
and obviously a number field named "SomeNumber" in my sitecore item. But the thing is I only get 0 value for SomeNumber when I input a number on the field and a null when not. Note that I'm getting right values for my string fields so I'm not sure why the number field doesn't work right.
Thanks

MVC3 validation with data-annotation?

Is it possible to use a data-annotation attribute for dropdownlists to be valid, only if the user selects an option different from one with the value o (zero).
The option with value o (zero) is "Please Select an account", which is selected by default.
I used [Required] attribute to validate this dropdownlist, but it doesn't have any effect, because, how I said, by default the option with value o (zero)- "Please Select an account"- is selected.
Something like:
[Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")]
public int Value { get; set; }
Making a drop-down list required does not make sense. What you want required is that the user pick a value other than zero from the drop-down list. So what should be required is the SelectedAccount property. You should use the MVC helper method to bind the drop-down selected value to the SelectedAccount property:
#Html.DropdownListFor(m => m.SelectedAccount, new SelectList(Model.Accounts))
I am probably off on the syntax but you can look that up.
Now with regards to your other issue of ensuring that the value is not zero. Is the account number represented as a number or can it contain non-numeric characters? If it is a number then you should represent it as such in your code. And if it is truly a string, then the first value of your drop-down list should be an empty string and not zero.
If you decide that it is a number, then use the Range annotation to ensure that the value is greater than zero:
[Required]
[Range(1, Int32.MaxValue)]
public string SelectedAccountNumber {get;set;}
Hope that helps!

How to Range validation integer ASP.NET MVC 3

I'm using validations as below.
[Required(ErrorMessage = "Please provide 10 digit mobile number without spaces and without country code (+91)")]
[Integer(ErrorMessage = "Please provide 10 digit mobile number without spaces and without country code (+91)")]
[Range(1000000000, 9999999999, ErrorMessage = "10 digit mobile number only without spaces and without country code (+91)")]
[Display(Name = "Mobile Number")]
public int MobileNo { get; set; }
It is always failing the validations saying The value '9999999998' is invalid.. Am I doing something wrong?
Try this:
[RegularExpression("^[0-9]{10}$", ErrorMessage = "Invalid Mobile No")]
The maximum value that an Int32 type could store is 2,147,483,648. You are overflowing. Why are you using an integer type to represent a phone number? String seems more adapted.
The Max Value an Integer(Int32) can hold is 2,147,483,647. So you should better replace Int with Long or String.

Resources