Jquery DataTable中的复合ID

时间:2015-08-04 03:39:03

标签: asp.net-mvc datatable asp.net-mvc-5

我正在使用DataTable,我需要做一个访问行的链接。但该行有一个复合键

密钥由IdEnvironment(int)和Limit(十进制)

组成

这是我的模特:

public partial class ApprovalHierarchy
{
    [Required]
    [Display(Name = "Ambiente")]
    [DataTables(DisplayName = "Ambiente", MRenderFunction = "encloseInEmTag",Order =1)]
    public int IdEnvironment { get; set; }

    [Required]
    [Display(Name = "Límite de aprobación")]
    [DataTables(DisplayName = "Límite de aprobación", MRenderFunction = "encloseInEmTag",Order =1)]
    [RegularExpression("^[0-9]+([.][0-9]{1,2})?$", ErrorMessage = "El límite debe de ser un número con dos decimales")]
    public decimal Limit { get; set; }

    [Display(Name = "Nombre")]
    [Required]
    public string Name { get; set; }

    [Display(Name = "Descripción")]
    public string Description { get; set; }
    public System.DateTime CreationDate { get; set; }
    [Required]
    [Display(Name = "Moneda")]
    public Nullable<int> IdCurrency { get; set; }

    public virtual Environment Environment { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<AspNetUsers> AspNetUsers { get; set; }

    public virtual Currency Currency { get; set; }
}

}

当我放入DataTable Annotation`MRenderFunction =“encolseInEmTag”时 链接的ID更改但我需要将其作为参数中的复合键发送,以便在我的控制器中访问此方法:

    // GET: /ApprovalHierarchy/Details/5
    public ActionResult ModalDetails(int? id,decimal? limit)
    {
        if (id == null || limit ==null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        ApprovalHierarchy approvalHierarchy = db.ApprovalHierarchy.First(x => x.IdEnvironment==
        id && x.Limit == limit);
        if (approvalHierarchy == null)
        {
            return HttpNotFound();
        }
        return PartialView("Details",approvalHierarchy);
    }

1 个答案:

答案 0 :(得分:0)

我解决了在模型中创建新属性并在控制器中设置值的问题。

模型属性:

[Key,DataTables(MRenderFunction = "encloseInEmTag",Visible =false)]
public string CompositeId { get; set; }

控制器:

 row.CompositeId = query.ElementAt(i).IdEnvironment + "?Limit=" + query.ElementAt(i).Limit.ToString().Replace(",",".");