简体   繁体   中英

How to display count of products in search Brand on search engine in mvc 4

Want to show a count of Models in front of Brand Name like :

  1. Nokia (10)
  2. Phone (20)

I have use a foreach tobind a Brand Name Data. Below is a code of foreach

 @foreach (var item in this.Model.CompanyList)
 {
    <li><a href="javascript:void(0);" style="padding-top: 2px; padding-bottom: 2px;">
    <input type="checkbox" name="chkbrand" id="chkbrand" value="@item.Brand"/>
    @item.Brand <span class="badge badge-info">4</span></a>
    </li>
 }

I have done a code in Model public static int Count which I want to call from <span class="badge badge-info">4</span> and replace a count with 4 with the result. Below is the static code

public static int Count(string Company)
{
  int count = 0;
  using (var db = new Entities())
  {
     count = (from o in db.Models
               where o.Brand == Company
               from t in o.Name
               select t).Count();
            }
         return count;            
  }

How can I call the Modeal Public static function in foreach ?

您必须从Razor视图调用方法:

<span class="badge badge-info">@YourClass.Count(item.Name)</span></a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM