>应用程序中的服务器错误。传递到字典中的模型项的类型为“Develosoft4.Models.Cita”,但此字典需要“Develosoft4.Models.CitaFormViewModel”类型的模型项。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“Develosoft4.Models.Cita”,但此字典需要“Develosoft4.Models.CitaFormViewModel”类型的模型项。
来源错误:
在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。
这是抛出错误的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");
}
}
}
答案 0 :(得分:0)
传递到字典中的模型项的类型为“Develosoft4.Models.Cita”,但此字典需要“Develosoft4.Models.CitaFormViewModel”类型的模型项
看起来你正在从一个动作方法返回错误的模型类型。
//This is where I think the error is. It is expecting a CityFormViewModel instead of a Cita object
return View(citaModel);
答案 1 :(得分:0)
当您希望类型为Develosoft4.Models.Cita
的对象时,您尝试将模型传递给Develosoft4.Models.CitaFormViewModel
类型的对象。
你可能有一个强类型视图,所以你需要传递它所期望的类型。
检查你的控制器什么时候你应该有这样的东西:
return View(new Develosoft4.Models.CitaFormViewModel()
{
// initializers
});
不知道你的代码实际上是什么样的,所以这是在黑暗中刺伤:)
编辑:根据您添加的代码,Post
Create
版本的return View(cita);
版本返回的视图类型错误。
你这样做:
Create
当您的CitaFormViewModel
视图期待return View(new CitaFormViewModel(cita));
时,您应该这样做:
Get
只需点击Create
版{{1}}视图即可。