views:

41

answers:

2

Server Error in '/' Application.

The model item passed into the dictionary is of type 'Develosoft4.Models.Cita', but this dictionary requires a model item of type 'Develosoft4.Models.CitaFormViewModel'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'Develosoft4.Models.Cita', but this dictionary requires a model item of type 'Develosoft4.Models.CitaFormViewModel'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

This is the Create.aspx that throws the error:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Develosoft4.Models.CitaFormViewModel>" %>
    <h2>Create</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend></legend>




            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.materia)%>
            </div>
            <div class="editor-field">

                <%: Html.DropDownListFor(model => model.Cita.materia, Model.Materias)%>
                <%: Html.ValidationMessageFor(model => model.Cita.materia)%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.cubiculo)%>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.Cita.cubiculo, Model.Cubiculos)%>
                <%: Html.ValidationMessageFor(model => model.Cita.cubiculo)%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.profesor)%>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.Cita.profesor, Model.Profesores)%>
                <%: Html.ValidationMessageFor(model => model.Cita.profesor)%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.fecha)%>
            </div>
            <div class="editor-field">

                <%: Html.ValidationMessageFor(model => model.Cita.fecha)%>
                    <form>
   <input type="text" name="fecha" id="campofecha">
</form>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.horaInicio)%>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Cita.horaInicio)%>
                <%: Html.ValidationMessageFor(model => model.Cita.horaInicio)%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.horaFinal)%>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Cita.horaFinal)%>
                <%: Html.ValidationMessageFor(model => model.Cita.horaFinal)%>
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

This is CitaFormViewModel.cs

using System.Web.Mvc;

namespace Develosoft4.Models
{
    public class CitaFormViewModel
    {
        private static CubiculoRepository cubiculosRepository = new CubiculoRepository();
        private static MateriaRepository materiasRepository = new MateriaRepository();
        private static ProfesorRepository profesorRepository = new ProfesorRepository();

// Properties
        public Cita Cita { get; private set; }
public SelectList Cubiculos { get; private set; }
public SelectList Materias { get; private set; }
        public SelectList Profesores { get; private set; }
// Constructor
public CitaFormViewModel(Cita cita)
{
    Cita = cita;
    Cubiculos = new SelectList(cubiculosRepository.FindAllCubiculos(),"id","nombre", cita.cubiculo);
    Materias = new SelectList(materiasRepository.FindAllMaterias(), "id", "nombre", cita.materia);
    Profesores = new SelectList(profesorRepository.FindAllProfesores(), "id", "nombre", cita.profesor);
}

} }

CitaController.cs

using System;

using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Develosoft4.Models;

namespace Develosoft4.Controllers { public class CitaController : Controller { CitaRepository repository = new CitaRepository();

    //
    // GET: /Cita/
   [Authorize (Roles= "director")]
   public ActionResult Index(int page = 0)
    {

        const int pageSize = 10;

        var citas = repository.FindAllCitas();
        var paginatedCita = new PaginatedList<Cita>(citas,page,pageSize);
        return View(paginatedCita);
    }
    //
    // GET: /Cita/Details/2

    public ActionResult Details(int id)
    {
        Cita cita = repository.GetCita(id);

        if (cita == null)
            return View("NotFound");
        else
            return View("Details", cita);
    }
    //
    // GET: /Cita/Edit/2

    public ActionResult Edit(int id)
    {
        Cita cita = repository.GetCita(id);
        CitaFormViewModel viewModel = new CitaFormViewModel(cita);
        return View(viewModel);
    }

    //
    // POST: /Cita/Edit/2
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection formValues)
    {
        Cita cita = repository.GetCita(id);

        try
        {
            UpdateModel(cita);
            repository.Save();
            return RedirectToAction("Details", new { id = cita.id });
        }
        catch
        {
            //ModelState.AddRuleViolations(materia.GetRuleViolations());

            return View(cita);
        }
    }

    //
    // GET: /Cita/Create
    public ActionResult Create()
    {
       Cita cita = new Cita();
        return View( new CitaFormViewModel( cita));
    }
    //
    // POST: /Cita/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Cita cita)
    {
        if (ModelState.IsValid)
        {
            try
            {

                repository.Add(cita);
                repository.Save();
               return RedirectToAction("Details", new { id = cita.id });
            }
            catch
            {
                //ModelState.AddRuleViolations(materia.GetRuleViolations());
            }
        }

        return View(cita);
    }

    //
    // HTTP GET: /Cita/Delete/1

    public ActionResult Delete(int id)
    {

        Cita cita = repository.GetCita(id);

        if (cita == null)
            return View("NotFound");
        else
            return View();
    }

    // actitud
    // HTTP POST: /Cita/Delete/1

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Delete(int id, string confirmButton)
    {

        Cita cita = repository.GetCita(id);

        if (cita == null)
            return View("NotFound");

        repository.Delete(cita);
        repository.Save();

        return View("Deleted");
    }


}
}
A: 

The model item passed into the dictionary is of type 'Develosoft4.Models.Cita', but this dictionary requires a model item of type 'Develosoft4.Models.CitaFormViewModel'

It looks like you are returning the wrong model type from an action method.

//This is where I think the error is. It is expecting a CityFormViewModel instead of a Cita object
return View(citaModel);
Jesper Palm
A: 

Your trying to pass your model an object of type Develosoft4.Models.Cita when it is expecting an object of type Develosoft4.Models.CitaFormViewModel.

You probably have a strongly typed view so you need to pass it the type it is expecting.

Check your controller when you should have something like this at the end:

return View(new Develosoft4.Models.CitaFormViewModel()
    {
        // initializers
    });

No idea what you code actually looks like so this is stab in the dark :)

EDIT: Based on the code you added, it looks like your Post version of Create is returning the wrong type to the view.

You are doing this:

return View(cita); 

When your Create view is expecting a CitaFormViewModel so you should probably be doing:

return View(new CitaFormViewModel(cita));

Just lke you did in the Get version of the Create view.

Kelsey