DataAnnotation [Key] makes a field readonly, is there other? - data-annotations

The Key DataAnnotation makes a field behave as ReadOnly when I use it in a DataForm or DataGrid since it denotes a DataBase key, but what if I just want to make it readonly because it's a calculated field?
Is there another DataAnnotation, other than Key, to make a field behave as ReadOnly in a DataForm?

Try the Editable annotation, with AllowEdit as false.
[System.ComponentModel.DataAnnotations.Editable(AllowEdit = false)]

Related

Data annotation for enforcing that the value stored in a property of type string should actually be a numeric value

Is there an annotation that allows me to say that the following property must have a numeric value and then I want to specify a range for that value?
[DataType.???]
[Range(1990, 2015)]
public string AnniversaryYear { get; set;}
There's a reason I need this to be a string.
check the use of Data Annotations Extensions that simply extends the data annotations that have into the framework, also look this article that show how to use them: Introducing Data Annotations Extensions

Variable of type Long is required

I have one ViewModel that has property Id of type long without attribute [required]. This model I use for search.
Problem : Why always when i try to make search request and input for property Id leave empty i get validation error as Id field is required ?
Make your model property nullable.
public long? myprop { get; set; }

Can Date attribute allow null value?

I have created a Model file for my MVC project that contains the following:
Public Class Player
Public Property FirstName As String
Public Property LastName As String
Public Property GoesBy As String
Public Property Birthdate As Date
End Class
I didn't mark the Birthdate field as a Required field, but it won't accept nulls. How can I add a validation or something else to allow for nulls? Or is that not possible and I have to just insert 1900-01-01 or something similar?
Just make your property Public Property Birthdate As Date as nullable like shown below:
Public Property Birthdate As Nullable(Of Date)
and just make your database field which accepts Birthdate as nullable so that it can accept null values also.

ReadOnly attribute doesn't work in ASP.NET MVC Models

I have marked a property as readonly in the model class, like this:
public class RegisterModel
{
[Display(Name = "User name")]
[ReadOnly(true)]
public string UserName { get; set; }
...
}
and in my view:
#Html.EditorFor(m => m.UserName)
but when I run the application, the textbox is not readonly.
I know I can use html attributes in the view to make it readonly, but I would prefer if this can be done in the model class itself.
Can it be achieved?
[Update] I don't think it is possible without new { #readonly = "readonly" }.Readonly property specifies whether the property this attribute is bound to is read-only or read/write. Details Here.
But you could try for Custom Helpers or try Using Editable instead Readonly on the model and use the metadata property in your View.
[Editable(false)]
I guess you have already looked into
Does ReadOnly(true) work with Html.EditorForModel?
also
a fine article odetocode.com
ReadOnly attribute doesn't block the HMTL helper to display the field as an enabled input. It is an information that the only the MVC data Binder will respect.
It means that RegisterModel instance, which will be posted back after form submission by a user, will always has null value on it's UserName property, regardless of user's input in the appropriate field of the form.
ReadOnly attribute does not set the input to read-only.
Try this
Html.TextBoxFor(x => x.UserName, new { readonly = "readonly" })
If you're using a SETTER you have to use { get; private set; }. That will make it so the client can't change the value. You can also just use an HTML 5 input and mark it there.

How to validate that user enters string in textbox in asp mvc4

How to validate that user enters string in textbox in asp mvc4?
What to write in required tag?
[required]
Use the [RegularExpression] attribute if you want to limit the user to only typing in alphabetic characters.
More info on MSDN.
Here is a good link to a regular expression that you can use.
This example maybe helps:
public class CustomerMetaData
{
// Require that the Title is not null.
// Use custom validation error.
[Required(ErrorMessage = "Title is required.")]
public object Title;
// Require that the MiddleName is not null.
// Use standard validation error.
[Required()]
public object MiddleName;
}
There are many ways to do it
1) By using plain Javascript or JQuery to check if it has value before submiting the page
2) On controller method check if it has value
3) If you a using EF and your view binded to a model add attribute called [Required]to the property of that model.
What do you actually want to do?
Make sure that the object the server receives has correct data in it? Then you should use data attributes on your C# model. However what do you mean by "enters string"? If the user simply needs to enter any string, then [Required] works - this just means that there has to be some value entered. Do you only want to allow a specific set of characters, like the English alphabet? Then you need to use a RegularExpression attribute.
If you further specify what you actually want to do I am sure we can help you more.

Resources