Pull Attribute setName from another class in the Agenda view using the Idservico foreign key with Razor Asp.Net mvc

Asked

Viewed 121 times

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?

  • Edit your question and include your action and your Agendas and Service models

1 answer

0

You selected the list with the services but did not return it, switch 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 appears the attributes

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

  • In the View I put it like this: @Html.Displayfor(modelItem => item.Idservico) But this way appears the value of the ID field. If I put it like this: @Html.Displayfor(modelItem => item.Servico.Name) Nothing appears. What I’m doing wrong?

  • @Pedrotomaz, look at my answer, you need to leave your Actionresult as I reported, you get to give the Include in service, but are not returning the correct object to the View

  • I changed the Action as it changed, but it didn’t work. So I passed the View code to check if it was right. But I still can’t fix the problem.

  • @Pedrotomaz, please edit the question by adding how your controller is

Browser other questions tagged

You are not signed in. Login or sign up in order to post.