asp.net mvc dataannotions attaching attribute - asp.net-mvc

I am trying to attach an attribute to the particular property in the this case the FirstName but the problem is in this code it is attaching to the birthday datetime property as well . what might be the problem with this
public class CustomMetadataValidationProvider : DataAnnotationsModelValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
if ( metadata.PropertyName == "FirstName")
attributes = new List<Attribute>() { new RequiredAttribute() };
return base.GetValidators(metadata, context, attributes);
}
}
public class User
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Birthday { get; set; }
}
protected void Application_Start()
{
//ModelValidatorProviders.Providers.Clear();
//ModelValidatorProviders.Providers.Add(new CustomMetadataValidationProvider());
ModelValidatorProviders.Providers.Add(new CustomMetadataValidationProvider());
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
can someone can explain how GetValidators is working?

Your problem has nothing to do with your GetValidators method.
Value types like (int, decimal, DateTime, etc.) are required by default. Because otherwise the model binder cannot set their values if they are not sent with the request.
So you need to change your Birtday property to nullable if you don't want to be required:
public class User
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? Birthday { get; set; }
}

Related

ASP.NET Identity Foreign Key as Primary key?

In my ASP.NET MVC Application, I customized the Identity to use another table for storing the users' infos such as first name, last name and birthdate. Here's the association code:
public class ApplicationUser : IdentityUser
{
public virtual UserInfo Info { get; set; }
}
public class UserInfo
{
public int Id { get; set; }
public string FirstName { get; set;
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
But how do you use the primary key of the ApplicationUser class as the primary key of UserInfo so that you know which user accounts the infos belong to?
The primary Key of ApplicationUser will be of a default type String, so first you will need to change your dependent class to also have a key of type string.
public class UserInfo
{
public string Id { get; set; }
public string FirstName { get; set;
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
Then in your IdentityDbContext implementation you will need to create a mapping to create the 1:1 relationship you are after like so.
public class MyIdentityDbContext : IdentityDbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>().HasRequired(x => x.Info).WithRequiredDependent();
base.OnModelCreating(modelBuilder);
}
}
and that should work

Required Attribute and complex types mvc 5

I have a type defined as
public class Autocomplete
{
public Guid Id { get; set; }
public string Label { get; set; }
}
I then have a model with this type as
public class MPEngagementActivity : IActivity
{
[UIHint("Hidden")]
public Guid Id { get; set; }
[UIHint("Hidden")]
//TODO: GET THIS FROM THE LOGGED IN USER
public Guid CreatedBy { get; set; }
[UIHint("Hidden")]
public int ActivityType { get; set; }
[Required(ErrorMessage = "Please select an Organisation")]
[Display(Name="Constituency")]
public Autocomplete Organisation { get; set; }
[UIHint("ReadOnly")]
[Display(Name = "MP Office Default Contact")]
public String DefaultContact { get; set; }
[Display(Name = "MP Contact")]
public Autocomplete MainContact { get; set; }
}
As you can see one of the properties is marked as required, but when testing the model is coming back as valid even though the property was not set in the form, the id is coming back as 0s and the label empty.
How can I get mvc to properly validate this?
public class Autocomplete
{
public Guid Id { get; set; }
public string Label { get; set; }
// This should do the trick with the standard Required attribute
public static implicit operator string(Autocomplete ac)
{
return ac.Label;
}
// Optionally, if you want to use a custom required instead, this may be more correct
public override string ToString()
{
return Label;
}
}
Then you simply put the [Required] attribute on the Autocomplete property.
Set the [Required] attribute on the class's properties
public class Autocomplete
{
[Requried]
public Guid Id { get; set; }
[Required]
public string Label { get; set; }
}

How can I use my own User table instead of default UserProfile in MVC 4 ?

I want to create large user table (advance User Profile) and save user's data in my database context. So, I don't want to use 2 DbContexts in my project. When users register to site, they data (UserName, Password etc.) stores my own User table. My classes are like this:
public class ModelBase
{
public int Id { get; set; }
public DateTime CreateDate { get; set; }
public DateTime LastUpdateDate { get; set; }
}
public class User : ModelBase
{
public string UserName { get; set; }
public string Password{ get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public DateTime BirthDate { get; set; }
public string Specialty { get; set; }
}
public class News : ModelBase
{
public int UserId { get; set; }
public string Title { get; set; }
...
}
....
Context is so:
public class MyDBContext : DbContext
{
public MyDBContext()
{
Database.SetInitializer<MyDBContext>(new MyDBContextInitializer());
}
public DbSet<User> UserSet { get; set; }
public DbSet<News> NewsSet { get; set; }
public DbSet<Project> ProjectSet { get; set; }
public DbSet<Section> SectionSet { get; set; }
....
}
class MyDBContextInitializer : DropCreateDatabaseIfModelChanges<MyDBContext>
{
protected override void Seed(MyDBContext context)
{
base.Seed(context);
}
}
I replaced DbContext name with mine and changed connection name in default SimpleMembershipInitializer class like this:
....
Database.SetInitializer<MyDBContext>(null);
try
{
using (var context = new MyDBContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
WebSecurity.InitializeDatabaseConnection("MyDBContextConnection", "User", "Id", "UserName", autoCreateTables: true);
....
Finally, I changed RegisterModel and WebSecurity.CreateUserAndAccount() suitable my User class. But, it does not work.
How can I use my own User table for register to site?
You can have Asp.net Membership and your complex classes connected together.
with this approach you will save so much time because asp.net membership is much more robust(you don't need to think about Role and User management) and sure you can make use of existing open source project like this and add it to your project with minimum effort of time.
Then your class will have structure like :
public class CustomUserDetail : ModelBase
{
public string UserName { get; set; } // what you really need is this to be unique for each user in you data base
// public string Password{ get; set; } handled by asp.net Membership
public string FullName { get; set; }
// public string Email { get; set; } handled by asp.net Membership
public DateTime BirthDate { get; set; }
public string Specialty { get; set; }
}
Then you can can add extension method to IPrincipal like :
public static CustomUserDetail CustomUserDetail (this IPrincipal principal)
{
var repository = new YourUserDetailRepository();
return repository.GetCurrentUserDetail();
}
and finnaly in your code easily use
<p> #User.CustomUserDetail.FullName </p>

How can I enter in date and username automatically when updating storage?

I have the following class that's used by my MVC3 application. I would like
to simplify the updating of the class so that when a new class object is
created then the Created and CreatedBy fields get set automatically.
I'd also like to make it so that the Modified and ModifiedBy fields get
updated automatically.
Is there a way that I could do this?
The class is used in MVCnamespace Storage.Models
{
public class Topic : TableServiceEntity
{
[DisplayName("Partition Key")]
public override string PartitionKey { get; set; }
[DisplayName("Row Key")]
public override string RowKey { get; set; }
[DisplayName("Description")]
public string Description { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public String CreatedBy { get; set; }
public string ModifiedBy { get; set; }
}
}
Set the defaults in the constructor for the class
public class Topic
{
public Topic()
{
this.Created = DateTime.Now;
this.CreatedBy = UserName;
}
[DisplayName("Partition Key")]
public override string PartitionKey { get; set; }
[DisplayName("Row Key")]
public override string RowKey { get; set; }
[DisplayName("Description")]
public string Description { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public String CreatedBy { get; set; }
public string ModifiedBy { get; set; }
}
jonathan,
Jason's answer above with the logic contained within the constructors is a perfectly valid and clean way of doing this and I wouldn't argue with that (and have done it myself for more 'static' properties). However, given that there could be a timelapse between the creation of the object and the actual save, then you may also want to consider putting this logic into your controller (or service layer).
this would look roughly like this:
public ActionResult Create(MyCreateViewModel viewModel)
{
if (ModelState.IsValid)
{
viewModel.Entity.Created = DateTime.UtcNow;
_myService.Insert(viewModel.Entity);
_myService.SaveChanges();
return this.RedirectToAction(x => x.Index());
} else {
PopulateViewModel(viewModel);
return View(viewModel);
}
}
likewise, you may have a LastEdit datetime that you want to track. use the Edit action similarly:
public ActionResult Edit(MyEditViewModel viewModel)
{
if (ModelState.IsValid)
{
viewModel.Entity.LastEditDate= DateTime.UtcNow;
_myService.AttachAndUpdate(viewModel.Entity);
_myService.SaveChanges();
return this.RedirectToAction(x => x.Index());
} else {
PopulateViewModel(viewModel);
return View(viewModel);
}
}
just another approach to ensure that datetime related properties are truly reflected.
In this solution i think you may have to make big change in your Repository/Service layer
define an Interface like :
public interface IHistoryLog
{
DateTime Created { get; set; }
DateTime Modified { get; set; }
string CreatedBy { get; set; }
string ModifiedBy { get; set; }
}
then:
public class Topic:IHistoryLog
{
// Implement interface..
}
then create a generic service class:
public abstract class CRUDService<TModel>
{
protected CRUDService(DataContext dataContext)
{
// data context to do generic CRUD stuff
}
public virtual Save(TModel model)
{
if(model is IHistoryLog)
{
// assign Createdby and Created
}
}
public virtual Update(TModel model)
{
if(model is IHistoryLog)
{
// assign ModifiedBy and Modified
}
}
}

How to make a nullable property in EF Codefirst?

I have two POCOs in my "Bookshelf" test application:
/// <summary>
/// Represents a book
/// </summary>
public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public virtual Loaner LoanedTo { get; set; }
}
/// <summary>
/// Represents a Loaner
/// </summary>
public class Loaner
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Loans { get; set; }
}
Is there a way that my LoanedTo could be nullable? I mean a book isn't always loaned, right! I tried
public virtual Loaner? LoanedTo { get; set; }
But I get:
The type 'RebtelTests.Models.Loaner' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
So I must be thinking wrong somewhere, but I can't figure it out. Probably easy squeeze for you guys.
You don't need to do anything special. Classes are always nullable.
I just tried this (with MVC3):
In my Models directory:
namespace MvcApplication2.Models
{
public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public virtual Loaner LoanedTo { get; set; }
}
public class Loaner
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Loans { get; set; }
}
public class BookContext : System.Data.Entity.DbContext
{
public System.Data.Entity.DbSet<Book> Books { get; set; }
public System.Data.Entity.DbSet<Loaner> Loaners { get; set; }
}
}
In my HomeController:
namespace MvcApplication2.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
string message = "OK";
try
{
var context = new Models.BookContext();
var book = new Models.Book();
book.Title = "New Title";
book.Author = "New Author";
book.ISBN = "New ISBN";
context.Books.Add(book);
context.SaveChanges();
}
catch (Exception err)
{
message = err.ToString();
}
ViewBag.Message = message;
return View();
}
}
}
The connectionstring in Web.Config:
<add name="BookContext" connectionString="Data Source=|DataDirectory|BookContext.sdf" providerName="System.Data.SqlServerCe.4.0" />
When I run the application, the view displays "OK". This means that no exception was thrown. When I look in my App_Data folder, a BookContext.sdf file has been created. That database contains a table for the Books and the Loaners. The table for the Loaners is empty. The one for the Books contains one record:
ID: 1; Title: "New Title"; Author: "New Author"; ISBN: "New ISBN"; LoanerID: null
If you are talking about a simple property like int, bool, or float use int?, bool?, or float?
like
public int? ID { get; set; }
public bool? Exists { get; set; }
Couldn't you just use something like this
public virtual Nullable<Loaner> LoanedTo { get; set; }
That then should make LoanedTo a nullable property

Resources