Hola a todos:
Les explico que es lo que deseo hacer .
uso asp.net mvc, con entity framework y quiero hacer una validacion personalizada sobre un modelo, el cual les muestro:
public partial class Operations
{
public Operations()
{
this.Rol_Perfil = new HashSet<Rol_Perfil>();
}
public int Id { get; set; }
[Index("Idx_OperacionModulo", order: 1)]
[Required(ErrorMessage = "Name Operation Required")]
[Display(Name = "Name of Operation")]
[StringLength(10)]
[OperationExist] ------------------------------------> este hace referencia a la clase donde creo la validación
public string Operation { get; set; }
[Index("Idx_OperacionModulo", order: 2)]
[Required(ErrorMessage = "Select Modul required")]
[Display(Name = "Select a Modul")]
public int IdModuls { get; set; }
[ScriptIgnore]
public virtual Moduls Moduls { get; set; }
public virtual ICollection<Rol_Perfil> Rol_Perfil { get; set; }
}
Clase llamada OperationExist, donde quiero realizar la validacion utilizando las propiedades de DataAnnotation:
public class OperationExistAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
using (Vigoro_TI.Models.VIG0R0Entities1 db = new VIG0R0Entities1())
{
if (value != null)
{
string Operation = (string)value;
/////aqui necesito pasar el valoor del dropdown
int IdModulo = ??? --------------------------------------> no se como traer el value de IdModuls que contiene el
modelo, alguien me comento esto: tienes que recibir IdModule con reflexión de c#, pero no se como hacerlo, o bien si alguien tiene otra idea de como lograr que esta variable reciba el valor de IdModuls.
if (db.Operations.Where(d => d.Operation == Operation && d.IdModuls == IdModulo).Count() > 0)
{
return new ValidationResult("Operation with Modul Exist...");
}
}
}
return ValidationResult.Success;
}
}
adjunto tambien la vista:
@model Vigoro_TI.Models.Operations
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Operations</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Operation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Operation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Operation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IdModuls, "Select a Modul", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("IdModuls", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.IdModuls, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<p>
<input type="submit" value="Create" class="btn btn-sm btn-success" /> |
@Html.ActionLink("Back to List", "Index", new { }, new { @class = "btn btn-sm btn-secondary" })
</p>
</div>
</div>
Saludos a todos.