Web 2
Web 2
Steps :
1 create controller EntitynameController
2 inject services in controller
IServicEntity serviceEntity; , generate constructor
3 in action name , right click , generate view, razor view, select the right view
template (list for index crate for create ….)
select the right entity name
4/add view
Seach by date
index view
<form asp-action="index">
<fieldset>
<legend> Recherche</legend>
Saisir une date de départ : <input type="date" name="name" />
<input type="submit" value="Serach" />
</fieldset>
</form>
Get Controller
// GET: FlightController
public ActionResult Index(DateTime? name)
{
if (name == null)
return View(sf.GetAll());
else
return View(sf.GetMany(f =>
f.FlightDate.Date.Equals(dateDepart)));
}
Search by enum
index View
<form asp-action="index">
<fieldset>
<legend> Recherche</legend>
Saisir un type:
<select
asp-items="Html.GetEnumSelectList<E.ApplicationCore.Domain.enumname>
()" name="type" >
</select>
<input type="submit" value="Serach" />
</fieldset>
Get Controller
static enum :
in create View :
change the simple input with :
<select asp-for="Type" class="form-control"
asp-items="Html.GetEnumSelectList<E.ApplicationCore.Domain.enumName>
()"
></select>
dynamic List
step 1
config in controller (get create ) : creation of view bag
// GET: CompteController/Create
public ActionResult Create()
{
ViewBag.viewbagname = new SelectList(serviceBanque.GetAll(),
"primary key", "display prop");
return View();
}
step2
1/generate view
2/ change simple input to :
<select asp-for="entityFK" class="form-control" asp-items="
ViewBag.viewbagname"
></select>
using E.ApplicationCore.Domain;
using E.ApplicationCore.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace E.Web.Controllers{
public class EntityController : Controller {
IServiceEntity serviceEntity;
public CompteController(IServiceEntity serviceEntity,) {
this.serviceEntity = serviceEntity
}
// GET: CompteController index with search
public ActionResult Index(TypeCompte? type)
{
if(type==null)
return View(serviceCompte.GetAll());
return View(serviceCompte.GetMany(p => p.Type.Equals(type)));
}
// GET: CompteController index without search
public ActionResult Index() {
return View(serviceCompte.GetAll());
}
either with search or without, you can”t have
both / if u have multiple search , just add a
second one in the same index
);
}
// GET: CompteController/Details/5
public ActionResult Details(String id)
{
return View(serviceCompte.GetById(id));
// GET: CompteController/Create
public ActionResult Create()
{
if u have a Dynamic list in the create, add the view bag here, as
mentioned above(dynamic list section ) otherwise, leave it empty
return View();
}
// POST: CompteController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Compte c)
{
try
{
serviceCompte.Add(c);
serviceCompte.Commit();
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
return View(serviceCompte.GetById(id));
}
// POST: CompteController/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Type of primary key id, Compte c1)
{
try
{
serviceCompte.Update(c1);
serviceCompte.Commit();
return RedirectToAction(nameof(Index));
}
catch
{ return View(); } }
// GET: CompteController/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: CompteController/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(Type of primary key id, IFormCollection
collection)
{
try
{
serviceCompte.Delete(serviceCompte.GetById(id));
serviceCompte.Commit();
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
}
—------------------------
something that u don’t actually need
Shared ->Layout
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Plane"
asp-action="Index">plane index</a> </li>