Pull ServiceName attribute from another class in the Agenda view using the foreign key ServiceID with Razor Asp.Net mvc

0

I have a Index view that lists the schedules.

In this list the calendar attributes include a foreign key IDServico , however, the list only appears the IDServico index and needs to appear the Service Name .

>

The code that displays the index looks like this:

@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 looks 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);
        }
    }
}
    
asked by anonymous 03.08.2018 / 21:10

1 answer

0

You have selected the list with the services but did not return it, change to the following:

public ActionResult Index() 
{ 
    var agendas = db.Agendas.Include(t => t.Servico); 
    ViewBag.IDServico = new SelectList(db.Servicos, "ID", "Nome"); 
    return View(agendas); 
} 

You say:

  

In this list the attributes

I would advise you to read this question / answer: What is the difference between attribute and field in classes? to understand more about what is attribute.

    
09.08.2018 / 22:54