0
votes

I got this error Error 2 The type arguments for method 'System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

with the following code in my view

index.cshtml

 <div>
@for (int i = 0; i < Model.CartItems.Count; i++)
{ 
     <p>
        @Html.ValidationMessageFor(model => model.CartItems[i].Count)  <== error here
        </p> 
}
 </div>

and I got also an error in this code in the same view Error 3 The type arguments for method 'System.Web.Mvc.Html.InputExtensions.TextBoxFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IDictionary)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

    @{int ix = 0;}

    @foreach (var item in Model.CartItems)
    {
        <tr id="[email protected]">
            <td>
                @Html.ActionLink(item.Produit.Description, "Details", "Produit", new { id = 
                         item.ProduitId }, null)
            </td>
            <td>
                @item.Produit.Prix
            </td>
            <td>
                @Html.TextBoxFor(model => model.CartItems[ix].Count,      <=== error here
                    new  {style = "width:30px; text-align:right;",
                    onkeyup = "clearUpdateMessage();",
                    onchange = "clearUpdateMessage();"
                    }) 
            </td>
            <td>
                <a href="#" class="RefreshQuantity" data-id="@item.PanierId"
                   txt-id="CartItems_@(ix)__Count">Refresh quantity</a>&nbsp;|&nbsp;  /a>
            </td>
            <td>
                <a href="#" class="RemoveLink" data-id="@item.PanierId"> Enlever du panier >> 
                </a>
            </td>
        </tr>
        ix++;
    }

Panier.cs

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel.DataAnnotations;
 using System.Web.Mvc; 

 namespace Tp1WebStore3.Models
 {

   public partial class Panier
   {
    [Key]
    public int PanierId { get; set; }
    public int ProduitId { get; set; }

    [Required(AllowEmptyStrings = true, ErrorMessage = " ")]
    [Range(0, 100, ErrorMessage = "Quantité doit être entre 0 and 100")]
    [DisplayName("Quantité")]
    public int Quantite { get; set; }

    public System.DateTime DateCree { get; set; }
    public string CartId { get; set; }
    public decimal Prix { get; set; }

    public virtual Produit Produit { get; set; }
   }
}
1
Do you have an @model declaration? - SLaks
@SLaks I added the panier.cs I based my code on codeproject.com/Articles/336901/… - user3127986

1 Answers

0
votes

I finally found the problem

the declaration should be like this:

@Html.ValidationMessageFor(model => model.CartItems[i].Quantite)

@Html.TextBoxFor(model => model.CartItems[ix].Quantite,