Linak 문을 사용하여 검색 할 수있는 TextBox를 내보기에 만들려고합니다.보기에서 Linq를 사용하여 검색
보기에서 검색을 구현하려고하면 컴파일러에서 내 prods 목록을 찾을 수 없습니다.
컨트롤러에서 구현하려고하면 내 searchBox를 찾을 수 없습니다. 두 경우 모두 이해하지만 문제를 해결하는 방법을 이해하지 못합니다. 또는 컨트롤러 또는보기에서 검색을 구현하는 것이 가장 적합한 경우 ??
내 검색 기능을 시도한 코드에 주석을 추가했습니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Uppgift_1.Models;
namespace Uppgift_1.Controllers
{
public class ProductController : Controller
{
public ActionResult Index()
{
List<MyProduct> prods = new List<MyProduct>()
{
.....add Products......
};
// Linq search
var searchResults = (from s in prods where(s.ProductName == searchBox || s.ProductId == searchBox) select s).ToList();
return View(prods);
}
}
}
@model IEnumerable<Uppgift_1.Models.MyProduct>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@foreach (var x in Model)
{
<div>
<h3>@x.ProductName</h3>
Article# @x.ProductId
<h4>@x.PriceSell.ToString("c")</h4>
</div><hr />
}
<div>
<input type="text" name="searchBox" />
// Link search
@{
var searchResults = (from s in prods where (s.ProductName == searchBox || s.ProductId == searchBox)
select s).ToList();
}
</div>
</body>
</html>
코드에서'prods' 목록을 사용하여이 쿼리가 나는 것 같아요 문제는 두 번째입니까? 'prods'리스트를'Index()'메소드 밖에서 선언해야한다. –