Witam, chciałem się dowiedzieć, jak odwołać się z poziomu widoku do zmiennej zdefiniowanej w kontrolerze ?
Chciałbym móc wyszukiwać konkretną książkę z biblioteki, dlatego właśnie utworzyłem kontroler Search.
Przykładowy kod:
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab01.Models
{
public class BibliotekaModel
{
public string Ksiazka { get; set; }
public int Pozycja { get; set; }
}
}
Kontroler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using lab01.Models;
namespace lab01.Controllers
{
public class BibliotekaController : Controller
{
static List<BibliotekaModel> data = new List<BibliotekaModel>();
// GET: Biblioteka
public ActionResult Index(int? id = null)
{
ViewBag.data = data;
ViewBag.biblioteka = id != null ? data[id.Value] : null;
return View();
}
public ActionResult Add(string ksiazka, int pozycja)
{
data.Add(new BibliotekaModel() { Ksiazka = ksiazka, Pozycja = pozycja });
return RedirectToAction("Index");
}
public ActionResult Search(string szukana)
{
var ksiazka = data.Where(p => p.Ksiazka == szukana);
return View();
}
}
}
@{
ViewBag.Title = "Search";
}
<h2>Search</h2>
<form method="get" action="/Biblioteka/Search">
<table border="1">
<tr>
<th> Szukana pozycja </th>
<th> Znaleziono </th>
</tr>
<tr>
<td><input name="Szukana" type="text" /></td>
</tr>
</table>
</form>