I've been practicing on Asp.Net Core MVC recently. We were able to localize and globalise the ViewModel attributes before using below class and overriding. I've been looking for this solution over a week.
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
public LocalizedDisplayNameAttribute(string resourceId)
: base(GetMessageFromResource(resourceId))
{
}
private static string GetMessageFromResource(string resourceId)
{
//return value from whatever the source is.
}
}
My view model looks like
public class LoginViewModel
{
[CustomRequiredAttribute("Email")]
[LocalizedDisplayName("Email")]
[EmailAddress]
public string Email { get; set; }
[LocalizedDisplayName("Password")]
[CustomRequiredAttribute("Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
I copied the same LocalizedDisplay class and tried it on Asp.Net core but didn't work.
The problem is on view, application prints field name not the value from attribute.
Problem solved : The solation is on githubb https://github.com/dotnet/corefx/issues/11846#issuecomment-248148026
Related
I can't find annotation for MongoDb, what can in .Net Core model modify collection name. In SQL database it will be [Table("all_sessions")].
My model name and collection name are different. I have not change model or collection name.
public class Session
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[BsonElement("_id")]
public string Id { get; set; }
[BsonElement("expires")]
public string Expires { get; set; }
[BsonElement("session")]
public string Session { get; set; }
}
My collection name is all_sessions.
I expect get working Session model with all_sessions collection.
So time ago i was fancing similar issue and i've created my own implementation of that pattern.
So first part is to create custom attribute:
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class BsonCollectionAttribute : Attribute
{
private string _collectionName;
public BsonCollectionAttribute(string collectionName)
{
_collectionName = collectionName;
}
public string CollectionName => _collectionName;
}
Second part is to obtain the value of this attribute with reflection
private static string GetCollectionName()
{
return (typeof(T).GetCustomAttributes(typeof(BsonCollectionAttribute), true).FirstOrDefault()
as BsonCollectionAttribute).CollectionName;
}
I'am doing that in repository class, so example method from mthat class looks like that:
public async Task InsertOne(T model)
{
var collectionName = GetCollectionName();
var collection = Database.GetCollection<T>(collectionName);
await collection.InsertOneAsync(model);
}
In the end my model looks like:
[BsonCollection("Users")]
public class User
{
[BsonId]
[BsonElement("id")]
[BsonRepresentation(BsonType.String)]
public Guid Id { get; set; }
[BsonElement("name")]
public string Name { get; set; }
[BsonElement("email")]
public string Email { get; set; }
[BsonElement("password")]
public string Password { get; set; }
Hope that i've helped ;)
I got this error in my OData with asp.net core implementation during the runtime :The EDM instance of type '[XXX.Asset Nullable=True]' is missing the property 'externalId'.
The problem appear when I try to access the odata endpoint with the expand query: "/odata/v1/precinct?$expand=assets". It seems happening because I put the "ExternalId" property in my base class, its not happening if I put that property in the "Asset".
Below is my recent codes:
public abstract class Entity
{
public int Id { get; set; }
public string ExternalId { get; set; }
}
public class Precinct : Entity
{
public string Name { get; set; }
public string Description { get; set; }
public virtual IEnumerable<Asset> Assets { get; set; }
}
public class Asset : Entity
{
public string Name { get; set; }
public string Description { get; set; }
}
and here is my model configuration for ODATA
public class AssetModelConfiguration : IModelConfiguration
{
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
{
var org = builder.EntitySet<Asset>("asset").EntityType;
org.HasKey(x => x.ExternalId);
org.Ignore(x => x.Id);
}
}
The strange thing is if I put that ExternalId in "Asset" class, it is working. Id property is the primary key while the "ExternalId" is marked as AlternateKey in the DBModel configuration.
am I missing something in my odata configuration? already tried many things but couldn't find a good answer. Any help would be appreciated!
Using C#, ASP .Net MVC 4, Entity Framework 5 and Database First. How can I override a property or add a new property to a model class? Below you can see the "Type" as a Byte, but I can't display the byte to the users and I want to display a friendly name. The Type description is not in the database, it is stored in a dictionary.
namespace Cntities
{
public partial class myClass
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public byte Type { get; set; }
}
}
Since the class is a partial you can expand and add your own properties and methods just ensure its defined in the same location as your current partial file.
namespace Cntities
{
public partial class myClass
{
public string DisplayValue{ get {
return "Formated"; // An example
}
}
}
Take a look at http://msdn.microsoft.com/en-us/library/wa80x488.aspx
One question about ASP / MVC 2 Data Annotations.
I have ViewModels with inheritance:
public class CategoryModel
{
[Required]
[Display(Name = "XName")]
//[DisplayName("XXX")]
[StringLength(255)]
public virtual string Name { get; set; }
}
public class CategoryListModel : CategoryModel
{
[Display(Name = "WName")]
//[DisplayName("WWW")]
[StringLength(255)]
public new string Name { get; set; }
}
Then in Controller:
public ActionResult List()
{
CategoryListModel model = new CategoryListModel();
return View(model);
}
And then in typed View:
<%=Html.LabelFor(m => Model.Name)%>
Why [Display(Name="SomeName")] does not work, neither in base model nor inherited one?
Meanwhile [DisplayName("SomeName")] works fine including inheritance ...
Thanks, Art
i have used customised registration page in my asp.net mvc 2.0 application.For that i have used a view Model calss called UserProfile.cs inherited from Profile Base class.
Now i want to apply the server side validation to the UserProfile class.But it is not working.
My User Profile class is as follows,
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(UserProfile))]
public class UserProfile : ProfileBase
{
[Required]
public virtual string FirstName
{
get
{
return ((string)(this.GetPropertyValue("FirstName")));
}
set
{
this.SetPropertyValue("FirstName", value);
}
}
[Required]
public virtual string LastName
{
get
{
return ((string)(this.GetPropertyValue("LastName")));
}
set
{
this.SetPropertyValue("LastName", value);
}
}
[Required]
public virtual string LoginId
{
get
{
return ((string)(this.GetPropertyValue("LoginId")));
}
set
{
this.SetPropertyValue("LoginId", value);
}
}
}
I have added the System.ComponentModel.DataAnnotation reference.still it is not working .Please tell me why it is not working.
To add the data annotations to an existing model you can do is as follows:
[MetadataType(typeof(User_Validation))]
public partial class User
{
}
public class User_Validation
{
[Required, StringLength(50)]
public object FirstName { get; set; }
[Required, StringLength(50)]
public object LastName { get; set; }
}
In my case my original model is generated which is why I'm adding my validation using the MetadataTypeAttribute as shown.
My generated object would look something like:
public partial class User
{
public virtual string FirstName
{
get;
set;
}
public virtual string LastName
{
get;
set;
}
}
You can see that when using the extra class through medadata type that you need to add the properties as object and with the same name.
If you are not trying to add the validation to an existing model in this way then you don't need the MetadataTypeAttribute.
check out this question and asnwers -
validation