ASP.NET MVC Web form: 404 error

0

Working with ASP.NET Core 1.1 Visual Studio 2017 - EntityFramework 1.0.0, Microsoft .Net Framework 4.7.02046, SQL Server 2012 LocalDB C # I am making my first experience with these tools. I follow the example ContosoUniversity developed in the MS documentation.

The application (web form) deals with operating room reservations, it is an agenda. I have two entity sets related many to many .: Reservations and Professionals, there is an entity set that resolves the relationship: ProfessionalReservation.

namespace ReservaQ.Models
{
    public class ProfesionalPorReserva
    {
        public ProfesionalPorReserva() { }
        public int ProfesionalID { get; set; }
        public int ReservaID { get; set; }
        public int RolID { get; set; }
        //Navegation props
        public Profesional Profesional { get; set; }
        public Reserva Reserva { get; set; }
        public Rol Rol { get; set; }
    }
}

In the MVC scheme developed with scaffolding there is a ReservasController controller with its Views Index, Create, Edit, Details, Delete. I modified the BookingController in order to show the Professionals assigned to a Reserve and their Role (Surgeon, Assistant, Anesthetist, etc.) in the View Details. The professionals are presented as a list. Next to each Professional element I added a link that allows to make a Delete of said professional. That link sends the id of the Professional, the id2 of the reservation and the id3 of the Role to the ReservationController, Action Details, since said action is called a method that produces the Delete of the record of that reservation and that professional of the entity Set ProfessionalPorReserva. Up to here everything works fine. When performing the delete attempt (here is the problem) the controller returns to show the View Details without the professional removed But at this point I get a 404 error, and shows the Route that corresponds to the Step from the View to the Controller. If I operate on the page I return to the main menu and to Reservations / Details I check that the professional has disappeared from the list. I do not know where my error is. I use an auxiliary entity Set to pass through ViewModel the data to the Detail.cshtml.

ReservationsController.cs code action Details

   // GET: Reservas/Details/5
    public async Task<IActionResult> Details(int? id,int? id2, int? id3)
    {
        if (id == null)
        {
            return NotFound();
        }
        if (id2 != null)
        {
            await EliminarProfesionalAsync((int) id, (int) id2,(int) id3);
        }
        var reserva = await _context.Reservas
            .Include(r => r.ProfesionalPorReservas)
                .ThenInclude(navigationPropertyPath: p => p.Profesional)
            .Include(r => r.ProfesionalPorReservas)
                .ThenInclude(navigationPropertyPath: r => r.Rol)
            .Include(p => p.Paciente)
            .AsNoTracking()
            .SingleOrDefaultAsync(m => m.ReservaID == id);
        if (reserva == null)
        {
            return NotFound();
        }
        var profesionalesRol = new List<ProfesionalRol>();
        foreach(var p in reserva.ProfesionalPorReservas)
        {
            profesionalesRol.Add(new ProfesionalRol
            { AyN = p.Profesional.AyN,EnRol = p.Rol.EnRol, Orden = .RolID,Clave=p.Profesional.ID});
            };
        ViewData["Profesionales"] = profesionalesRol.OrderBy(p => p.Orden).ToList();
        return View(reserva);
    }

Remove the Professional method code within ReservasController

private async Task EliminarProfesionalAsync(int id, int id2, int id3)
        {          
            ProfesionalPorReserva profesionalPRToDelete = new ProfesionalPorReserva()
            {   ReservaID = id2,
                ProfesionalID = id,
                RolID = id3
            };
            _context.Entry(profesionalPRToDelete).State =     EntityState.Deleted;
            await _context.SaveChangesAsync();
         }

View Details Code (part where the list appears and the link to Delete)

@model ReservaQ.Models.Reserva

@{
ViewData["Title"] = "Details";
List<ReservaQ.Models.ReservaQViewModel.ProfesionalRol> profs=ViewBag.Profesionales;}

<h2>Details</h2>

<div>
<h4>Reserva</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.FechaCirugia)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.FechaCirugia)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Quirofano)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Quirofano)
    </dd>
    <dt>
        @Html.DisplayNameFor(Model => Model.Paciente.AyN)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Paciente.AyN)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Diagnostico)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Diagnostico)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Procedimiento)
    </dt>
    //omito algunos campos

    @* acá comienza el listado de profesionales -rol *@
    <dt>
        Profesionales
    </dt>
    <dd>
        <table class="table">
            <tr>
                Profesional
            </tr>
            <tr>
                 - Rol
            </tr>
            @foreach (var item in profs)
            {
                <tr>
                    <td>
                        @item.AyN -- @item.EnRol
                    </td>
                    <td>
                        <a asp-action="Details" 
                        asp-route-id="@item.Clave" 
                        asp-route-id2="@Model.ReservaID" 
                        asp-route-id3="@item.Orden">Delete</a>
                    </td>
                </tr>
            }
        </table>
    </dd>
    @* acá termina *@
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.ReservaID">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>

Error message obtained

Can not find this page (localhost)
No web page found for the address link .
• Search localhost 50184 Booking Details on Google HTTP ERROR 404   1 is the id of the professional
20 is the id of the reservation
1 is the Role of the professional

That entity (1,20,1) has been removed from Entity Set Professional by Booking

    
asked by María Celia Ibarra 21.11.2017 в 00:31
source

2 answers

0

Hello, if you have the driver based on Microsoft.AspNetCore.Mvc you can decorate it with the attribute:

[Route("api/Reservas")]
public class ReservasController : Controller
{
    [HttpGet("{id}/reserva/{id2}/rol/{id3}")]
    public async Task Details(int? id, int? id2, int? id3){
    ...
    }
}

URL: link

If you use Controller based on System.Web.Mvc

@Html.ActionLink("Nombre de aplicación", "Details", "Reservas", new { id = 5, id2= 6, id3 =9 }, new { @class = "navbar-brand" })

Controller

public class ReservasController : Controller
{
    public async Task<ActionResult> Details(int? id, int? id2, int? id3)
    {
        return View();
    }
}

URL: link

    
answered by 21.11.2017 / 10:02
source
1

The problem is an untidiness on my part. The arguments do not correspond to the parameters. they were exchanged id and id2. It was already solved.

    
answered by 21.11.2017 в 19:14