EnVisageOnline/Main-RMO/Source/EnVisage/Models/RoleModel.cs

67 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using EnVisage.Code;
using System.Web.Mvc;
namespace EnVisage.Models
{
public class RoleModel : IBaseModel<AspNetRole>
{
//ENV-612 START
public Guid Id { get { return _Id; } set { this._Id = value; if (value == Guid.Empty) this._isNew = true; else this._isNew = false; } }
//public Guid Id { get; set;}
public bool isNew { get { return _isNew; } }
private bool _isNew = false;
private Guid _Id = Guid.Empty;
//ENV-746 END
[Required]
[Remote("IsUnique", "Role", HttpMethod = "POST", AdditionalFields="Id", ErrorMessage = "The role name entered is already in use please enter a different role name")]
public string Name { get; set; }
public int AspNetUsersCount { get; set; }
/// <summary>
/// Casts a <see cref="AspNetRole"/> obect to the object of type <see cref="RoleModel"/>.
/// </summary>
/// <param name="obj">A <see cref="AspNetRole"/> object.</param>
/// <returns>A <see cref="RoleModel"/> object filled with data from db.</returns>
public static explicit operator RoleModel(AspNetRole obj)
{
if (obj == null)
return null;
var model = new RoleModel
{
Id = Guid.Parse(obj.Id),
Name = obj.Name,
AspNetUsersCount = obj.AspNetUsers.Count
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Once a model is created a the generated guid needs to be
/// applied to this model so that future saves of the projects and categorys
/// have correct model ID.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void setIDForNewModel(Guid g)
{
this._Id = g;
this._isNew = true;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(AspNetRole dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
if (string.IsNullOrEmpty(dbObj.Id) && !Guid.Empty.Equals(Id))
dbObj.Id = Id.ToString();
dbObj.Name = Name;
}
}
}