Server Error in '/' Application.Asp.net MVC 2应用程序错误
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'.
源错误:
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.
这是Create.aspx抛出错误:
<%@ 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>
这是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");
}
}
}
任何帮助将非常感激。 谢谢大家的进步。 – Daniel 2010-08-18 05:11:05
编辑您的问题并向我们显示出现此错误的代码。 – 2010-08-18 05:24:22
欢迎来到Stack Overflow。如果下面的答案之一帮助回答你的问题,你应该将其标记为接受的答案。 – Kelsey 2010-08-19 04:53:01