0
I have a view Index which lists the schedules.
In this list the agenda attributes including a foreign key appear IDServico
, however, the list appears only the index of IDServico
and I need the Service name.
The code displaying the index is thus:
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.AutorizacaoAgenda)
</td>
<td>
@Html.DisplayFor(modelItem => item.Data)
</td>
<td>
@Html.DisplayFor(modelItem => item.Hora)
</td>
<td>
@Html.DisplayFor(modelItem => item.Disponibilidade)
</td>
@*Aqui é o código para mostrar o IDServico*@
<td>
**@Html.DisplayFor(modelItem => item.IDServico)**
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
My Controller
is like this:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ProjetoBarber.Models;
namespace ProjetoBarber.Controllers
{
public class AgendaController : Controller
{
private BarbeariaDB db = new BarbeariaDB();
// GET: Agenda
public ActionResult Index()
{
var agendas = db.Agendas.Include(t => t.Servico);
ViewBag.IDServico = new SelectList(db.Servicos, "ID", "Nome");
return View(agendas);
}
// GET: Agenda/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Agenda agenda = db.Agendas.Find(id);
if (agenda == null)
{
return HttpNotFound();
}
return View(agenda);
}
// GET: Agenda/Create
public ActionResult Create()
{
ViewBag.IDServico = new SelectList(db.Servicos, "ID", "Nome");
return View();
}
// POST: Agenda/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Data,Hora,IDServico,IDUsuario")] Agenda agenda)
{
if (ModelState.IsValid)
{
db.Agendas.Add(agenda);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.IDServico = new SelectList(db.Servicos, "ID", "Nome",agenda.IDServico);
return View(agenda);
}
// GET: Agenda/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Agenda agenda = db.Agendas.Find(id);
if (agenda == null)
{
return HttpNotFound();
}
return View(agenda);
}
// POST: Agenda/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Data,Hora,IDServico,IDUsuario")] Agenda agenda)
{
if (ModelState.IsValid)
{
db.Entry(agenda).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(agenda);
}
// GET: Agenda/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Agenda agenda = db.Agendas.Find(id);
if (agenda == null)
{
return HttpNotFound();
}
return View(agenda);
}
// POST: Agenda/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Agenda agenda = db.Agendas.Find(id);
db.Agendas.Remove(agenda);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Guys, I tried this way but without success: <td> @Html.Displayfor(modelItem => item.Servico.Name) </td> The controller is as follows: // GET: Agenda public Actionresult Index() { var agendas = db.Agendas.Include(t => t.Service); Viewbag.Idservico = new Selectlist(db.Services, "ID", "Name"); Return View(db.Agendas.Tolist()); } Why does not list the service in place of the Dice?
– Pedro Tomaz
Edit your question and include your action and your Agendas and Service models
– Netinho Santos