先生,我正在做网格视图的代码,但它显示错误

时间:2016-08-03 10:26:43

标签: asp.net-mvc-4

先生这个控制器中的控制器显示错误,请在代码下方检查。

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        ProductDetailModel obj = new ProductDetailModel();
        obj.ProductList = ProductData("");
        return View(obj);
    }

    [HttpPost]
    public ActionResult SearchResult(string SearchKey)
    {
        ProductDetailModel obj = new ProductDetailModel();
        obj.ProductList = ProductData(SearchKey);
        string strTable = "";
        strTable = strTable + "< table>< thead>< tr>< th>Id</ th >< th >Product_Name</ th >";
        strTable = strTable + "< th >Product_Detail</ th >< th >price< / th></ tr></ thead>< tbody>";


        foreach (var item in obj.ProductList)
        {
            strTable = strTable + "< tr >";
            strTable = strTable + "< td >" + item.Id + "</ td >";
            strTable = strTable + "< td >" + item.Product_Name + "</ td >";
            strTable = strTable + "< td >" + item.Product_Detail + "</ td >";
            strTable = strTable + "< td>" + item.price + "</ td >";
            strTable = strTable + "</ tr >";
        }
        strTable = strTable + "</ tbody ></ table >";
        return View(strTable);
    }

    public List<product> ProductData(string SearchKey)
    {
        List<product> objProduct = new List<product>();
        gridEntities objDemoEntities = new gridEntities();
        var productItem = from data in objDemoEntities.grids
                          where SearchKey == "" ? true : data.Product_Details.Contains(SearchKey)
                          select data;

            foreach (var item in productData)
            {
                objProduct.Add(new product
                {
                    Id = item.Id,
                    Product_Name = item.Product_Name,
                    Product_Detail = item.Product_Details,
                    price = (int)item.price
                });
            }
            return objProduct;
        }

先生,这是错误 错误1 Foreach无法对“方法组”进行操作。你打算调用'方法组'吗? C:\ Users \ anand.kumar \ Documents \ Visual Studio 2013 \ Projects \ grid01 \ grid01 \ Controllers \ HomeController.cs 58 17 grid01 错误2 foreach语句无法对“方法组”类型的变量进行操作,因为“方法组”不包含“GetEnumerator”的公共定义C:\ Users \ anand.kumar \ Documents \ Visual Studio 2013 \ Projects \ grid01 \ grid01 \ Controllers \ HomeController.cs 58 17 grid01

1 个答案:

答案 0 :(得分:1)

按以下方式更改您的每个:

foreach (var item in productItem)
{
   //code
}